Reputation: 3
I need to understand one usage of pthread_mutex_lock()
and pthread_cond_wait()
and pthread_cond_signal()
.
I have seen a piece of code where a function, say for example, CallANumber()
is invoked from main()
and inside this CallANumber()
function pthread_mutex_lock()
is used along with pthread_cond_wait()
and then release by pthread_mutex_unlock()
and there is another function, say for example, WaitForResponse()
, inside this function pthread_mutex_lock()
along with pthread_cond_signal()
has been called and released by pthread_mutex_unlock()
.
But I have not found any pthread_create()
call inside the source base.
Is it possible to call Pthread_mutex_lock/unlock()
and pthread_cond_wait/signal()
APIs without a pthread_create()
function getting never been called ?
Upvotes: 0
Views: 99
Reputation: 33747
There are two reasons for using these functions in programs which are not multi-threaded:
The functions are called from generic code, perhaps in a library, and this library needs to perform synchronization in case the process is multi-threaded (which the library authors do not know). Without synchronization, the library might not work as expected in a multi-threaded program.
The synchronization happens across processes instead of threads, using process-shared mutexes and process-shared condition variables.
Upvotes: 3