Edit : @ninjalj's suggestion to replace while-loop using a condition variable is good advice if you use while-loop to wait until the program state is reached. However, if you use while-loop to do the work until the program state is reached, then ...
You must wrap the " lock-mutex; examine variable; unlock mutex" code in a utility function, and your while-loop condition can then call that function. For example, a utility function can be written as shown in the following code:
int valueOfSharedVariable()
{
int status;
int result;
status = pthread_mutex_lock(&mutex);
assert(status == 0);
result = sharedVariable;
status = pthread_mutex_unlock(&mutex);
assert(status == 0);
return result;
}
Then your while-loop condition can be written as follows:
while (valueOfSharedVariable() < 10) {
...
}
source
share