pthread_barrier_init,pthread_barrier_wait Introduction

Source: Internet
Author: User

The Pthread_barrier series functions are defined in <pthread.h> for multi-threaded synchronization, which contains three functions:

--pthread_barrier_init ()

--pthread_barrier_wait ()

--pthread_barrier_destroy ()

So what is pthread_barrier_* for? How do these three functions work with each other?

Pthread_barrier_* actually only do and only do one thing, is to act as a railing (barrier means railing). The image is that the number of threads arriving in front of the same railing, until all threads to the line, and then remove the railing and release. 1) The INIT function is responsible for specifying the number of threads to wait, 2) the Wait () function is called by each thread, and it tells the railing "I'm in front of the starting line." Wait () Executes the end railing to check if everyone is in front of the railing, and if it is, the railing disappears. All threads continue to execute the next code; if not, all threads that have been to wait () stop at the function, leaving the thread that does not execute to wait () to continue execution ; 3) The Destroy function releases the resources for the INIT request.

1 Examples of usage scenarios:

The biggest feature of this "railing" mechanism is that the last wait action is the most important, like the starting gun in a race, where everyone has to wait before it comes. So in practice, pthread_barrier_* is often used to let all threads wait for the "starting gun" to ring and then act together. For example, we can generate 100 threads with Pthread_create (), and each child thread will go into the callback function immediately after being created. But we may not want them to do this, because the main process is not ready yet, and the other threads that work with them are not ready, we want them to request the thread space in the callback function, stop after initialization, wait for the main process to release a "start" signal, and then all the threads to start executing the business logic code.

Solution:

To solve the above scenario problem, we can specify n+1 wait at init, where n is the number of threads. Instead, the header of each thread executing function calls Wait (). This way 100 pthread_create () end up with the wired routine stop waiting for the last Wait () function to be called. This wait () is good for the main process to call when it feels right. The last Wait () is the sound of the starting gun.

2 Function Prototypes:

#include <pthread.h>

int Pthread_barrier_init (pthread_barrier_t *restrict barrier, const pthread_barrierattr_t *restrict attr, unsigned count);

int pthread_barrier_wait (pthread_barrier_t *barrier);

int Pthread_barrier_destroy (pthread_barrier_t *barrier);

Parameter explanation:

pthread_barrier_t, is a count lock, the operation of the lock is contained within three functions, we do not care or direct operation. Just instantiate an object and throw it to it just fine.

pthread_barrierattr_t, set the property of the lock, set NULL to allow the function to use the default property.

Count, the number of waits you want to specify.

3 Examples of programs:

#include <pthread.h>

Class thisclass{

Private

Static void* dosomething (void* Arg)

{

thisclass* pinstance = (thisclass*) arg;

Pthread_barrier_wait (pinstance->barrier);//All threads are blocked here

Here's what the thread is specifically about to do

//...

}

Protected

pthread_barrier_t barrier;

int thread_num; = 100;

pthread_t* thread;

Public

int init () {//Generate 100 waiting threads

int ret =-1;

pthread_attr_t attr;

Pthread_attr_init (&ATTR);

do{

if (Thread_num = = 0

|| (thread = (pthread_t*) malloc (Thread_num *sizeof (pthread_t))) = = NULL)

Break

Pthread_barrier_init (&barrier,null, Thread_num + 1); 100+1 a waiting

for (i=0; i<thread_num; i++)

if (Pthread_create (Thread+i, &attr, dosomething, this))

Break 100 Create successful threads all go into the above dosomething function execution

if (i!= thread_num)

Break

ret= 0;

}while (False)

return ret;

}

int Activate () {//etc Everything is scheduled to call this function. The starting gun "Bang!" ”

Pthread_barrier_wait (barrier);

Return0;

}

}

See here for an English explanation of 43 functions, with more detailed syntax:

Http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_barrier_wait.html

Http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_barrier_init.html

Pthread_barrier_init,pthread_barrier_wait Introduction

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.