Long time no see Apue, today a friend asked about a mutex problem, and turned to the previous discussion of things, in order not to let themselves forget, the past things summed up.
First of all, look at the Internet in a lot of places on the pthread_cond_wait () Description:
Condition variable
A condition variable is a mechanism for synchronizing a global variable shared between threads, consisting mainly of two actions: one thread waits for the condition of the condition variable to hang, and the other thread makes the condition set (the conditional signal). To prevent competition, the use of conditional variables is always combined with a mutex.
1. Create and Log off
A conditional variable, like a mutex, has two ways to create static dynamics, using the Pthread_cond_initializer constant in static mode, as follows:
pthread_cond_t Cond=pthread_cond_initializer
The Pthread_cond_init () function is called dynamically, and the API is defined as follows:
int Pthread_cond_init (pthread_cond_t *cond, pthread_condattr_t *cond_attr)
Although the POSIX standard defines a property for a condition variable but is not implemented in Linuxthreads, the cond_attr value is usually null and is ignored.
Logging off a condition variable requires a call to Pthread_cond_destroy (), which can be logged out only if no thread is waiting on the condition variable, otherwise the ebusy is returned. Because the condition variables of the Linux implementation do not allocate any resources, the logoff action only includes checking for a wait thread. The API is defined as follows:
int Pthread_cond_destroy (pthread_cond_t *cond)
2. Wait and inspire
int pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex)
int pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, const struct TIMESPEC)
There are two ways to wait conditions: unconditionally wait for pthread_cond_wait () and wait pthread_cond_timedwait (), where the timing waits if the condition is not met at a given moment, then return to Etimeout and end the wait. where abstime in the same sense as the time () system calls the absolute times form, 0 GMT January 1, 1970 0:0 0 seconds.
Either way, you must work with a mutex to prevent multiple threads from simultaneously requesting pthread_cond_wait () (or pthread_cond_timedwait (), the same below) competition conditions (Race Condition). A mutex mutex must be a normal lock (PTHREAD_MUTEX_TIMED_NP) or an adaptive lock (PTHREAD_MUTEX_ADAPTIVE_NP) and be invoked pthread_cond_wait () The front must be Cheng by this line (Pthread_mutex_lock ()), and before the update condition waits for the queue, the mutex remains locked and the thread hangs into the wait before it unlocks. Before the condition is satisfied and leaves the pthread_cond_wait (), the mutex is locked back to correspond to the locking action before entering the pthread_cond_wait ().
There are two forms of excitation conditions, pthread_cond_signal () activates a thread waiting for the condition, one is activated in the queue while there are multiple waiting threads, and pthread_cond_broadcast () activates all waiting threads.
Now take a look at a typical application: Read the annotations.
#include <pthread.h> #include <unistd.h> static pthread_mutex_t mtx = Pthread_mutex_initializer;
static pthread_cond_t cond = Pthread_cond_initializer;
struct node {int n_number; struct node *n_next;} *head = NULL;
/*[thread_func]*/static void Cleanup_handler (void *arg) {printf ("cleanup handler of second thread./n");
Free (ARG);
(void) Pthread_mutex_unlock (&MTX);
} static void *thread_func (void *arg) {struct node *p = NULL;
Pthread_cleanup_push (Cleanup_handler, p); while (1) {pthread_mutex_lock (&MTX); This mutex is primarily used to guarantee pthread_cond_wait concurrency while (head = = NULL) {//This while to specifically illustrate that a single pthread_cond_wait function is perfect , why is there a while (head = = NULL)? Because the thread in the pthread_cond_wait may be awakened unexpectedly, if the head!= NULL at this time, it is not what we want. This time, should let the thread continue to enter Pthread_cond_wait pthread_cond_wait (&cond, &MTX); Pthread_cond_wait will first remove the Pthread_mutex_lock locked before the MTX, and then block the wait for column hibernation until it is awakened again (most of the time is the waiting condition is awakened, wake up, the process will first lock first pthread _mutex_lock (&MTX), and then read the resource//using this process is more clear/*block-->unlock-->wait () return-->l
ock*/} p = head;
Head = head->n_next;
printf ("Got%d from front of queue/n", p->n_number);
Free (p); Pthread_mutex_unlock (&MTX);
The critical section data operation completes, releases the mutual Exclusion Lock} pthread_cleanup_pop (0);
return 0;
int main (void) {pthread_t tid;
int i;
struct node *p; Pthread_create (&tid, NULL, THREAD_FUNC, NULL);
Child threads will wait for resources, similar to producers and consumers, but consumers here can be multiple consumers, not just ordinary individual consumers, this model is simple but powerful/*[tx6-main]*/for (i = 0; i < i++) {
p = malloc (sizeof (struct node));
P->n_number = i; Pthread_mutex_lock (&MTX);
Need to operate head this critical resource, first lock, p->n_next = head;
head = p;
Pthread_cond_signal (&cond); Pthread_mutex_unlock (&MTX);
Unlock sleep (1); printf ("Thread 1 Wanna End". So cancel thread 2./n "); Pthread_cancel (TID); With regard to Pthread_cancel, there is a little extra explanation that it is to terminate the child thread from the outside, the child thread will exit the thread at the nearest cancellation point, and in our code, the nearest cancellation point must be pthread_cond_wait ().
About the cancellation point of information, interested in Google, here is not much to say Pthread_join (Tid, NULL);
printf ("All done--exiting/n");
return 0; }