---

IBM developerWorks: POSIX threads explained, Part 3 – Improve efficiency with condition variables

In this article, the last of a three-part series on POSIX
threads, Daniel takes a good look at how to use condition
variables. Condition variables are POSIX thread structures that
allow you to “wake up” threads when certain conditions are
met.
You can think of them as a thread-safe form of
signalling. Daniel wraps up the article by using all that you’ve
learned so far to implement a multi-threaded work crew
application.”

“I ended my previous article by describing a particular dilemma:
how does a thread deal with a situation where it is waiting for a
specific condition to become true? It could repeatedly lock and
unlock a mutex, each time checking a shared data structure for a
certain value. But this is a waste of time and resources, and this
form of busy polling is extremely inefficient. The best way to do
this is to use the pthread_cond_wait() call to wait on a particular
condition to become true.”

“It’s important to understand what pthread_cond_wait() does —
it’s the heart of the POSIX threads signalling system, and also the
hardest part to understand.”


Complete Story

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends, & analysis