Linux Conditional Variable routine

Source: Internet
Author: User

# Include <br/> pthread_mutex_t mutex = pthread_mutex_initializer; <br/> pthread_cond_t cond = pthread_cond_initializer; <br/> void * thread1 (void *); <br/> void * thread2 (void *); <br/> int I = 1; <br/> main (void) <br/>{< br/> pthread_t T_A; <br/> pthread_t T_ B; <br/> pthread_create (& T_A, null, thread1, (void *) null ); <br/> pthread_create (& T_ B, null, thread2), (void *) null); <br/> pthread_join (T_ B, n Ull); <br/> pthread_mutex_destroy (& mutex); <br/> pthread_cond_destroy (& Cond); <br/> exit (0 ); <br/>}< br/> void * thread1 (void * junk) <br/>{< br/> for (I = 1; I <= 9; I ++) <br/>{< br/> pthread_mutex_lock (& mutex); <br/> if (I % 3 = 0) <br/> pthread_cond_signal (& Cond); <br/> else <br/> printf ("thead1: % d/N", I ); <br/> pthread_mutex_unlock (& mutex); <br/> printf ("up unlock mutex/N"); <br/> usleep (50 ); <br/>}< br/>} <Br/> void * thread2 (void * junk) <br/> {<br/> while (I <9) <br/>{< br/> pthread_mutex_lock (& mutex); <br/> if (I % 3! = 0) <br/> pthread_cond_wait (& cond, & mutex); <br/> printf ("thread2: % d/N", I ); <br/> pthread_mutex_unlock (& mutex); <br/> printf ("Down ulock mutex/N"); <br/> usleep (50); <br/>}

 

 

 

Copyright statement:
Reprinted Article please indicate the original source http://blog.csdn.net/feiyinzilgd/archive/2010/12/30/6108417.aspx
Contact tan Haiyan or go to tan Haiyan's personal homepage to leave a message.

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.