Multi-threaded synchronization

Source: Internet
Author: User
Tags mutex

Multi-threaded synchronization

One, what is synchronization

Multiple threads are executed in the specified order, which is thread synchronization.

1.1 Thread synchronization Mode one

Thread synchronization can be achieved by mutual exclusion lock, but the execution efficiency is lower (SYNC.C)

1#include <stdio.h>2#include <pthread.h>3#include <string.h>4 5 6pthread_t thread[2];7 intNumber =0; 8 pthread_mutex_t Mut;9 Ten  One voidStudenta () A { -     inti; -      for(i=0;i<5; i++) the     {   -Pthread_mutex_lock (&mut); -         //sweeping -number++; +         if(number>=5 ) -printf"student A has the finish his work! \ n");  +          APthread_mutex_unlock (&mut);  at         //rest for 1 seconds . -Sleep1); -     }  -     //Exit - Pthread_exit (NULL); - } in  - voidSTUDENTB () to {         +      while(1) -     {   thePthread_mutex_lock (&mut); *         if(number>=5)//judge whether a classmate has finished sweeping 5 times $         {   Panax Notoginseng             //perform a drag-and-drop -Number =0;  theprintf"student B has finish he work \ n"); +              APthread_mutex_unlock (&mut); the             //Exit +              Break;  -         }  $         Else $         {   -Pthread_mutex_unlock (&mut); -             //Sleep 2 seconds theSleep2);  -         }    Wuyi     } thePthread_exit (NULL);//Exit - } Wu  - intMain () About {    $     //Initialize Mutex lock -Pthread_mutex_init (&mut,null); -  -     //Create a classmate thread APthread_create (&thread[0],null,studenta,null); +      the     //Create a Class B thread -Pthread_create (&thread[1],null,studentb,null); $          the     //waiting for a classmate thread to end thePthread_join (thread[0],null); the      the     //wait for Class B thread to end -Pthread_join (thread[1],null); in          the     return 0; the}

1.2 thread synchronization mode two

Use of condition variables to increase CPU utilization (SYNC1.C)

1#include <stdio.h>2#include <pthread.h>3#include <string.h>4 5 6pthread_t thread[2];7 intNumber =0; 8 pthread_mutex_t Mut;9pthread_cond_t cond_ready=Pthread_cond_initializer;Ten  One voidStudenta () A { -     inti; -      for(i=0;i<5; i++) the     {   -Pthread_mutex_lock (&mut); -         //sweeping -number++; +         if(number>=5 ) -         { +printf"student A has the finish his work! \ n");  A              at             //Notify Class B -Pthread_cond_signal (&Cond_ready);  -         }     -Pthread_mutex_unlock (&mut);  -         //rest for 1 seconds . -Sleep1); in     }  -     //Exit to Pthread_exit (NULL); + } -  the voidSTUDENTB () * {         $ Panax NotoginsengPthread_mutex_lock (&mut); -     if(number<5) thePthread_cond_wait (&cond_ready, &mut);  +Number =0;  APthread_mutex_unlock (&mut); theprintf"student B has a finish his work! \ n");  +      -Pthread_exit (NULL);//Exit $ } $  - intMain () - {    the     //Initialize Mutex lock -Pthread_mutex_init (&mut,null);Wuyi  the     //Create a classmate thread -Pthread_create (&thread[0],null,studenta,null); Wu      -     //Create a Class B thread AboutPthread_create (&thread[1],null,studentb,null); $          -     //waiting for a classmate thread to end -Pthread_join (thread[0],null); -      A     //wait for Class B thread to end +Pthread_join (thread[1],null); the          -     return 0; $}

1.3 Item variables

Initialization

pthread_cond_t Cond_ready=pthread_cond_initializer;

Wait condition ripe:

Pthread_cond_wait (&cond_ready, &mut);

Set Condition Maturity:

Pthread_cond_signal (&cond_ready);

Multi-threaded synchronization

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.