Pthread_barrier_init, pthread_barrier_wait, pthread_barrier_destroy

Source: Internet
Author: User

Pthread_barrier_init, pthread_barrier_wait, pthread_barrier_destroy

The pthread_barrier series functions are defined in <pthread. h> and used for multi-thread synchronization. They contain three functions:

-- Pthread_barrier_init ()

-- Pthread_barrier_wait ()

-- Pthread_barrier_destroy ()

So what is pthread_barrier _ * used? How can these three functions be used together?

Pthread_barrier _ * In fact, only one thing is to act as a railing ). In the image, multiple threads that have arrived are blocked in front of the same railing until all threads arrive at the same time. 1) The init function specifies the number of threads to wait. 2) The wait () function is actively called by each thread. It tells the railing that "I am at the starting line ". When wait () executes the end railing, it checks whether all the people are in front of the railing. If yes, the railing disappears and all threads continue to execute the next code. If not, all threads that have reached wait () will stop in the function, and the remaining threads that have not been executed to wait () will continue to execute. 3) the destroy function will release the resources requested by init.

1. Use Cases:

The biggest feature of this "railing" mechanism is that the last wait action is the most important. Just like the starting gun during a race, everyone has to wait before it comes. So in actual use, pthread_barrier _ * is often used to let all threads wait for the "starting gun" to sound and then act together. For example, we can use pthread_create () to generate 100 threads. Each subthread automatically enters the callback function immediately when it is created. But we may not want them to do this, because the main process is not ready yet, and other threads that work with them are not ready yet, we want them to wait for the main process to release a "start" signal after applying for thread space in the callback function and stopping after initialization, and then all threads start to execute the business logic code.

Solution:

To solve the problem in the above scenario, we can specify n + 1 wait in init, where N is the number of threads. In each thread, wait () is called at the beginning of the function execution (). In this way, after the 100 pthread_create () ends, all threads stop and wait for the last wait () function to be called. This wait () is called by the main process when it feels appropriate. Finally, this wait () is the starting gun.

2 function prototype:

# 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 description:

Pthread_barrier_t is a counting lock. All the operations on this lock are included in three functions. We do not need to care about this lock and cannot directly operate it. You only need to instantiate an object and drop it.

Pthread_barrierattr_t. Set the lock attribute to null so that the function can use the default attribute.

Count, the number of waits you want to specify.

3 program example:

# 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

// The following are the specific tasks of the thread.

//...

}

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 wait

For (I = 0; I <thread_num; I ++)

If (pthread_create (thread + I, & ATTR, dosomething, this ))

Break; // 100 successful create threads all enter the above dosomething function execution

If (I! = Thread_num)

Break;

Ret = 0;

} While (false)

Return ret;

}

Int activate () {// and so on. The starting gun is "Bang !"

Pthread_barrier_wait (barrier );

Return0;

}

}

4 For an English description of the three functions, see here. There is a more detailed syntax introduction:

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

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

Reprinted please indicate the source: su ran Xu blog, http://hi.baidu.com/new/hehehehello

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.