Conditional variable pthread_cond_t in a thread

Source: Internet
Author: User

#include <pthread.h> #include <stdio.h> #include <stdlib.h>pthread_mutex_t mutex = pthread_mutex_ initializer;/* Initialize the mutex */pthread_cond_t cond = pthread_cond_initializer;/* Initialize the 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 end */Pthread_mutex_destroy (&am    P;mutex);    Pthread_cond_destroy (&cond); Exit (0);}        void *thread1 (void *junk) {for (i=1;i<=6;i++) {printf ("\n1111111111 I was%d 111111111111\n", i);        Pthread_mutex_lock (&mutex);/* Lock Mutex */printf ("Thread1:lock%d\n", __line__);            if (i%3==0) {printf ("Thread1:signal 1 line:%d i is%d \ n", __line__,i);   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) {printf ("2222222222222 in Pthread2 I was%d 222222222222222222 \ n", i);  Pthread_mutex_lock (&mutex);p rintf ("Thread2:lock line:%d i is%d\n", __line__,i), if (i%3!=0) {printf ("thread2:wait            1%d\n ", __line__); Pthread_cond_wait (&cond,&mutex);/* Unlocks the mutex and waits for cond to change */printf ("Thread2:wait 2%d\n", __line__);p rintf ("        Thread2:i is%d\n\n ", i);} Pthread_mutex_unlock (&mutex);p rintf ("Thread2:unlock%d\n\n", __line__); sleep (1);}}

Compile:

GCC Test.c-o test-lpthread

Logic:

The main function creates two threads


Thread 1: Accumulate I, each time mutually exclusive lock when I is a multiple of 3 to send change the condition signal to thread 2;


Thread 2: When I <6, the mutual exclusion lock operation, when not a multiple of 3 to wait for the adjustment to change the signal, blocking the thread;


When 1=1;

Thread 2:i not divisible by 3 blocking wait condition change signal;
Thread 1: normal cumulative i=2


i=2
Thread 2: Blocking
Thread 1: Not divisible by 3, normal cumulative


I=3
Thread 2: Blocking
Thread 1: Divide evenly, accept to send the condition change signal;
Thread 2 unlocked, normal operation sleep a second


I=4
Thread 2:i equals four blocking
Thread 1: Not divisible by 3, normal cumulative

I=5
Thread 2:i equals four blocking
Thread 1: Not divisible by 3, normal cumulative

I=6
Thread 2: Receive a signal to unlock
Thread 1: Sending a signal

Run:


2222222222222 in Pthread2 I was 1 222222222222222222
Thread2:lock line:46 i is 1
Thread2:wait 1 48

1111111111 i is 1 111111111111
Thread1:lock 27
Thread1:unlock 35


1111111111 I is 2 111111111111
Thread1:lock 27
Thread1:unlock 35


1111111111 i is 3 111111111111
Thread1:lock 27
Thread1:signal 1 line:29 i is 3
Thread1:signal 2 31
Thread1:unlock 35

Thread2:wait 2 53
THREAD2:I is 3

Thread2:unlock 57

2222222222222 in Pthread2 I was 3 222222222222222222
Thread2:lock line:46 i is 3
Thread2:unlock 57


1111111111 I is 4 111111111111
Thread1:lock 27
Thread1:unlock 35

2222222222222 in Pthread2 I was 4 222222222222222222
Thread2:lock line:46 I is 4
Thread2:wait 1 48

1111111111 i is 5 111111111111
Thread1:lock 27
Thread1:unlock 35


1111111111 I is 6 111111111111
Thread1:lock 27
Thread1:signal 1 line:29 i is 6
Thread1:signal 2 31
Thread1:unlock 35

Thread2:wait 2 53
THREAD2:I is 6

Thread2:unlock 57

Reference Link: http://blog.csdn.net/zclongembedded/article/details/7337729

Conditional variable pthread_cond_t in a thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.