"Turn" pthread_cond_t how to use

Source: Internet
Author: User

Original link: http://blog.csdn.net/zclongembedded/article/details/7337729#0-tsina-1-61587-397232819ff9a47a7b7e80a40613cfe1

Recently read the "UNIX Environment Advanced Programming" multi-threaded synchronization, see him example of the condition variable pthread_cond_t how to use, Leng did not understand, had to find a code on the Internet, ran to run, just to figure out

[CPP]View Plaincopyprint?
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. pthread_mutex_t mutex = Pthread_mutex_initializer; / * Initialize the mutex * /
  5. pthread_cond_t cond = Pthread_cond_initializer; / * Initialize condition variable * /
  6. void *thread1 (void *);
  7. void *thread2 (void *);
  8. int i=1;
  9. int main (void)
  10. {
  11. pthread_t t_a;
  12. pthread_t T_b;
  13. Pthread_create (&t_a,null,thread1, (void *) NULL); /* Create process t_a*/
  14. Pthread_create (&t_b,null,thread2, (void *) NULL); / * Create process t_b*/
  15. Pthread_join (T_a, NULL); / * Wait for process t_a to end * /
  16. Pthread_join (T_b, NULL); / * Wait for process t_b to end * /
  17. Pthread_mutex_destroy (&mutex);
  18. Pthread_cond_destroy (&cond);
  19. Exit (0);
  20. }
  21. void *thread1 (void *junk)
  22. {
  23. for (i=1;i<=6;i++)
  24. {
  25. Pthread_mutex_lock (&mutex); / * Lock the mutex * /
  26. printf ("Thread1:lock%d/n", __line__);
  27. if (i%3==0) {
  28. printf ("Thread1:signal 1%d/n", __line__);
  29. Pthread_cond_signal (&cond); / * Condition change, send signal, notify T_b process * /
  30. printf ("Thread1:signal 2%d/n", __line__);
  31. Sleep (1);
  32. }
  33. Pthread_mutex_unlock (&mutex); / * Unlock Mutex * /
  34. printf ("Thread1:unlock%d/n/n", __line__);
  35. Sleep (1);
  36. }
  37. }
  38. void *thread2 (void *junk)
  39. {
  40. while (I<6)
  41. {
  42. Pthread_mutex_lock (&mutex);
  43. printf ("Thread2:lock%d/n", __line__);
  44. if (i%3!=0) {
  45. printf ("Thread2:wait 1%d/n", __line__);
  46. Pthread_cond_wait (&cond,&mutex); / * Unlock the mutex and wait for cond to change * /
  47. printf ("Thread2:wait 2%d/n", __line__);
  48. }
  49. Pthread_mutex_unlock (&mutex);
  50. printf ("Thread2:unlock%d/n/n", __line__);
  51. Sleep (1);
  52. }
  53. }
[CPP]View Plaincopyprint?
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. pthread_mutex_t mutex = Pthread_mutex_initializer; / * Initialize the mutex * /
  5. pthread_cond_t cond = Pthread_cond_initializer; / * Initialize condition variable * /
  6. void *thread1 (void *);
  7. void *thread2 (void *);
  8. int i=1;
  9. int main (void)
  10. {
  11. pthread_t t_a;
  12. pthread_t T_b;
  13. Pthread_create (&t_a,null,thread1, (void *) NULL); /* Create process t_a*/
  14. Pthread_create (&t_b,null,thread2, (void *) NULL); / * Create process t_b*/
  15. Pthread_join (T_a, NULL); / * Wait for process t_a to end * /
  16. Pthread_join (T_b, NULL); / * Wait for process t_b to end * /
  17. Pthread_mutex_destroy (&mutex);
  18. Pthread_cond_destroy (&cond);
  19. Exit (0);
  20. }
  21. void *thread1 (void *junk)
  22. {
  23. For (i=1;i<=6;i++)
  24. {
  25. Pthread_mutex_lock (&mutex); / * Lock the mutex * /
  26. printf ("Thread1:lock%d/n", __line__);
  27. if (i%3==0) {
  28. printf ("Thread1:signal 1%d/n", __line__);
  29. Pthread_cond_signal (&cond); / * Condition change, send signal, notify T_b process * /
  30. printf ("Thread1:signal 2%d/n", __line__);
  31. Sleep (1);
  32. }
  33. Pthread_mutex_unlock (&mutex); / * Unlock Mutex * /
  34. printf ("Thread1:unlock%d/n/n", __line__);
  35. Sleep (1);
  36. }
  37. }
  38. void *thread2 (void *junk)
  39. {
  40. While (i<6)
  41. {
  42. Pthread_mutex_lock (&mutex);
  43. printf ("Thread2:lock%d/n", __line__);
  44. if (i%3!=0) {
  45. printf ("Thread2:wait 1%d/n", __line__);
  46. Pthread_cond_wait (&cond,&mutex); / * Unlock the mutex and wait for cond to change * /
  47. printf ("Thread2:wait 2%d/n", __line__);
  48. }
  49. Pthread_mutex_unlock (&mutex);
  50. printf ("Thread2:unlock%d/n/n", __line__);
  51. Sleep (1);
  52. }
  53. }
#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++) {pthread_mutex_lock (&mutex);/* Locks the Mutex */printf ("Thread1:lo CK%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);p rintf ("Thread2:lock%d/n", __line __), 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__);} Pthread_mutex_unlock (&mutex);p rintf ("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 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 the execution or the line Cheng get executed, in the end that line enters upgradeable get executed, Depending on the operation of the system scheduling, want to deliberately let the thread 2 first execution, you can let the thread 21 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.

at this point, however, thread two does not run immediately , because the line Cheng is still waiting for the mutex lock to be released, so the thread continues to go 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) releases the mutex lock and enters the next loop.

"Turn" pthread_cond_t how to use

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.