Transfer from http://blog.csdn.net/hongmy525/article/details/5194006
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
pthread_mutex_t Mutex = pthread_mutex_initializer;/* Initialize Mutex */
pthread_cond_t cond = pthread_cond_initializer;/* Initialize condition variable */
void *thread1 (void *);
void *thread2 (void *);
int i=1;
int main (void)
{
pthread_t t_a;
pthread_t T_b;
Pthread_create (&t_a,null,thread1, (void *) NULL);/* Create Process t_a*/
Pthread_create (&t_b,null,thread2, (void *) NULL); /* Create process t_b*/
Pthread_join (T_a, NULL);/* Wait for process t_a to end */
Pthread_join (T_b, NULL);/* Wait for process t_b to end */
Pthread_mutex_destroy (&mutex);
Pthread_cond_destroy (&cond);
Exit (0);
}
void *thread1 (void *junk)
{
for (i=1;i<=6;i++)
{
Pthread_mutex_lock (&mutex);/* Lock Mutex */
printf ("Thread1:lock%d/n", __line__);
if (i%3==0) {
printf ("Thread1:signal 1%d/n", __line__);
Pthread_cond_signal (&cond);/* condition change, send signal, notify T_b process */
printf ("Thread1:signal 2%d/n", __line__);
Sleep (1);
}
Pthread_mutex_unlock (&mutex);/* Unlock Mutex */
printf ("Thread1:unlock%d/n/n", __line__);
Sleep (1);
}
}
void *thread2 (void *junk)
{
while (I<6)
{
Pthread_mutex_lock (&mutex);
printf ("Thread2:lock%d/n", __line__);
if (i%3!=0) {
printf ("Thread2:wait 1%d/n", __line__);
Pthread_cond_wait (&cond,&mutex);/* Unlock mutex and wait for cond to change */
printf ("Thread2:wait 2%d/n", __line__);
}
Pthread_mutex_unlock (&mutex);
printf ("Thread2:unlock%d/n/n", __line__);
Sleep (1);
}
}
Compile:
[Email protected] threads]$ gcc thread_cond.c-lpthread-o TCD
The following are the results of the program operation:
[Email protected] threads]$/TCD thread1:lock thread1:unlock 40
Thread2:lock thread2:wait 1 Thread1:lock-Thread1:unlock 40
Thread1:lock thread1:signal 1 thread1:signal 2 Thread1:unlock 40
Thread2:wait 2 Thread2:unlock 61
Thread1:lock Thread1:unlock 40
Thread2:lock thread2:wait 1 Thread1:lock-Thread1:unlock 40
Thread1:lock thread1:signal 1 thread1:signal 2 Thread1:unlock 40
Thread2:wait 2 Thread2:unlock 61
Here are two key functions in the pthread_cond_wait and pthread_cond_signal functions.
In this example:
The line Cheng first executes, obtains the mutex lock, prints, then releases the mutex lock, and then blocks itself for 1 seconds. line Cheng at this time and thread one should be concurrent execution , here is a key point, why is the thread at this time is concurrent execution, because at this time do not do any interference, there is no way to determine whether the line Cheng first get execution or line Cheng get execution, in the end that line enters upgradeable get executed, depending on the operation system scheduling, want to deliberately let the thread 2 first execution, Can let the thread 21 come out, first sleep a second. In the case of concurrent execution, the line Cheng enters the loop first, then obtains the lock, at which time the thread two is estimated to execute, blocking at Pthread_mutex_lock (&mutex); This line of statements is until thread 1 releases the mutex lock Pthread_mutex_unlock (&MUTEX);/* Unlocks mutex */Then thread two is executed, acquires Metux lock, satisfies if condition, to pthread_cond_wait (&cond,&mutex);/* Wait */Here the thread two blocks, not only wait for the cond variable to change, but also release the mutex lock , because the reading did not pay attention, so here card for a long time. After the mutex lock is released, thread 1 finally obtains the mutex lock, which has to continue running, when the condition of thread 1 's if (i%3==0) is satisfied, sends a signal through pthread_cond_signal, tells the thread waiting for the variable of cond (this scenario is thread two), The COND condition variable has changed.
However, at this point, thread two does not get run immediately , because the line Cheng is still waiting for the release of the mutex lock, so thread one continues down until the thread releases the mutex lock, thread Two can stop waiting, print the statement, and then go down through Pthread_mutex_unlock (&mutex) Release the mutex lock and enter the next loop.
Linux Multithreading: Condition variables