Linux Thread synchronization------barriers

Source: Internet
Author: User

A barrier is a synchronization mechanism that coordinates the parallel work of multiple threads in Linux, allowing each thread to wait until all the cooperating threads reach a point and then continue to execute from that point, Pthread_join is a barrier but allows only one thread to wait, Pthread_ Barrier allows any number of threads to wait!

Pthread_barrier_init (pthread_barrier_t * barrier, attribute, unsigned int barrier count value);

Pthread_barrier_wait (pthread_barrier_t * barrier): Call the Count value in each thread and block the current thread here until the count value is set at the initialization value;

Thread Barrier usage Examples:

1, if a 8 million of the array to sort, in a thread to complete the need for a long time, if the use of a thread barrier, 8 threads to the 1 million number of heap sorting, and then in the main thread of the merge merge operation, time-consuming will save 6 times times!

2, the following example is the initialization of an array of 8 million elements, respectively, using 1, 4, 8 threads to complete, found that the use of 8 threads is the shortest time.

/* Program function: Create a dynamic array, assign an initial value to the array, create a num_thread thread, initialize an array of length size, pass (thread barrier synchronization) and print the entire array value when all threads have finished processing. Compares the time spent by a time count.  When using 8 threads, sort took 0.0958 seconds when four threads are used thread-1249961072 done job.thread-1260450928 do job.thread-1239471216 done job.thread-1270944880 done Job.sort took 0.1104 seconds when taking a thread thread-1239983216 done Job.sort took 0.2567 seconds*/#in Clude <pthread.h> #include <stdlib.h> #include <stdio.h> #define SIZE 8000000L//Array length # define Num_ Thread 8//number of threads # define SIZE_PER (Size/num_thread)//The array length to be handled by each thread pthread_barrier_t barrier;//define barrier int *a;/* thread handler for each thread * /void * Thr_fun (void *arg) {Long n = (long) Arg;long i;for (i=n;i<n+size_per;i++) {a[i] = i;} printf ("Thread%d done job.\n", pthread_self ());p thread_barrier_wait (&barrier); return ((void *) 1);} int main () {pthread_t tid;struct timeval start,end;long long startusec,endusec;double elapsed;int i;a = (int *) malloc (SIZE  *sizeof (int)); Dynamically allocated array pthread_barrier_init (&AMP;BARRIER,NULL,NUM_THREAD+1);//Initialize the thread barrier count to the number of child threads plus the main thread gettimeofday (&start,null);//Get Start time for (i=0;i<num_thread;i++) {pthread_create (&tid,null,thr_fun, (void *) (I*size_per) );//Create Child thread}pthread_barrier_wait (&barrier);//wait for all child threads to finish Gettimeofday (&end,null);//Get End time for (i=0;i<  size;i++)//print array contents//printf ("%d", A[i]); startusec = start.tv_sec * 1000000 + start.tv_usec;endusec = end.tv_sec * 1000000 + end.tv_usec;elapsed = (double) (endusec-startusec)/1000000.0;//the time spent calculating processing printf ("Sort took%.4f seconds\n", elapsed); return 0;}

Linux Thread synchronization------barriers

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.