• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
KaiserJohan

Deadlock dilemma with condition variables

4 posts in this topic

Hello,

I have a problem with condition variables; they are causing a deadlock in my code.
I have a bunch of threads running the "Worker" method (using Boost::bind and boost::function), and when I destroy my threadpool, I do TerminateAllWorkers().

About one out of ten times it causes a deadlock, so there must be something fundamentally wrong with the way I use the condition variables.

[CODE]
void ThreadPool::TerminateAllWorkers()
{
mMutex->Lock();
mDesiredNumThreads = 0;
mMutex->Unlock();
mCondVar_NewTaskOrKillWorker->Broadcast();
while (mNumThreads > 0)
{
mLogger.LogInfo() << "TerminateAllWorkers::mCondVar_WorkDoneOrWorkerKilled->Wait()" << std::endl;
mCondVar_WorkDoneOrWorkerKilled->Wait();
}
}


void ThreadPool::Worker(void* arg)
{
bool running = true;
while (running)
{
mMutex->Lock();
Task task = NULL;
// either terminate the worker, or check for tasks
if (mDesiredNumThreads < mNumThreads)
{
running = false;
mNumThreads--;
}
else
{
// if there is a task, pop it
if (!mScheduledTasks.empty())
{
task = mScheduledTasks.front();
mScheduledTasks.erase(mScheduledTasks.begin());
}
}
mMutex->Unlock();



// if task, run and then signal task is complete
if (task)
{
task();
mCondVar_WorkDoneOrWorkerKilled->Signal();
}

// terminate if requested, otherwise wait for new task
if (running)
{
mLogger.LogInfo() << "Worker::mCondVar_NewTaskOrKillWorker->Wait()" << std::endl;
mCondVar_NewTaskOrKillWorker->Wait();
}
else
{
mLogger.LogInfo() << "Worker::mCondVar_WorkDoneOrWorkerKilled->Signal()" << std::endl;
mCondVar_WorkDoneOrWorkerKilled->Signal();
}
}
}

[/CODE]


I am testing on Windows platform, by the way. Mutex lock/unlock is wrappers around WaitForSingleObject/ReleaseMutex, and here's simply ConditionVariable wait/broadcast/signal:

[CODE]void ConditionVariable::Wait()
{
mCondVarState = ConditionVariable::WAITING;
#if defined _WIN32 || _WIN64
WaitForSingleObject(mCondVarHandle, INFINITE);
#else
mMutex.Lock();
pthread_cond_wait(&mCondVarHandle, &mMutex.GetNativeHandle());
mMutex.Unlock();
#endif
mCondVarState = ConditionVariable::READY;
}
void ConditionVariable::Signal()
{
#if defined _WIN32 || _WIN64
SetEvent(mCondVarHandle);
ResetEvent(mCondVarHandle);
#else
mMutex.Lock();
pthread_cond_signal(&mCondVarHandle);
mMutex.Unlock();
#endif
}
void ConditionVariable::Broadcast()
{
#if defined _WIN32 || _WIN64
SetEvent(mCondVarHandle);
ResetEvent(mCondVarHandle);
#else
mMutex.Lock();
pthread_cond_broadcast(&mCondVarHandle);
mMutex.Unlock();
#endif
}[/CODE]
0

Share this post


Link to post
Share on other sites
Thanks for the reply!
Some subsequent questions:

[quote]Waiting on a condition variable should internally unlock a mutex and ensure it is locked when the wait call returns. Your implementation doesn't seem to be doing that, here.[/quote]


Could you elaborate on this one? I'm not sure I follow. I suppose I am missing than a mutex variable for the windows-variant, but why should it be locked when the wait call returns?


[quote]For example, here SetEvent could wake a waiting thread, which could then immediately wait on the condition variable again and be instantly let through, before ResetEvent is called. This is not the correct behaviour. Upon waiting again, the thread should block until a subsequent signal or broadcast awakens it.[/quote]


I understand; but, someone has to call ResetEvent() to reset the signal - how would you prevent a thread from slipping inbetween the SetEvent() and ResetEvent()? Edited by KaiserJohan
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0