反省:pthread_barrier_init(),pthread_barrier_destroy()和pthread_barrier_wait()

来源:互联网 发布:ubuntu添加阿里云源 编辑:程序博客网 时间:2024/04/28 19:35
     #include <pthread.h>
     int pthread_barrier_destroy(pthread_barrier_t *barrier);
     int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, int count);
     int pthread_barrier_wait(pthread_barrier_t *barrier);
     The pthread_barrier_init() function will initialize barrier with
     attributes specified in attr, or if it is NULL, with default attributes.
     The number of threads that must call pthread_barrier_wait() before any of
     the waiting threads can be released is specified by count.  The
     pthread_barrier_destroy() function will destroy barrier and release any
     resources that may have been allocated on its behalf.
     The pthread_barrier_wait() function will synchronize calling threads at
     barrier.  The threads will be blocked from making further progress until
     a sufficient number of threads calls this function.  The number of
     threads that must call it before any of them will be released is deter-
     mined by the count argument to pthread_barrier_init().  Once the threads
     have been released the barrier will be reset

确实这几个函数平常都没有用到,一般的作线程间的同步也就用到了mutex和condition_variable,还真的没有注意到这些个函数。虽然现在知道了这几个函数的功能,但是还没有能和实际情况联系起来,不知道在什么样的环境中需要用到。 按照描述,这个barrier的作用就像集体接力赛一样,一群人各自按照各自的路线像同一地点进发(pthread_barrier_wait()),当每个人到达该地点时就停下来等待其他的人到达,当所有的人(count个人)都到达后就再次出发干各自的事去了(可能是另一个目的地,也可能比赛就结束了,也可能……,总之是全新的一个开始)。

 

另外几个关于barrier的属性的几个函数:

      #include <pthread.h>
     int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
     int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared);
     int pthread_barrierattr_init(pthread_barrierattr_t *attr);
     int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared);
     The pthread_barrierattr_init() function will initialize attr with default
     attributes.  The pthread_barrierattr_destroy() function will destroy attr
     and release any resources that may have been allocated on its behalf.
     The pthread_barrierattr_getpshared() function will put the value of the
     process-shared attribute from attr into the memory area pointed to by
     pshared.  The pthread_barrierattr_setpshared() function will set the
     process-shared attribute of attr to the value specified in pshared.  The
     argument pshared may have one of the following values:
     PTHREAD_PROCESS_PRIVATE  The barrier object it is attached to may only be
            accessed by threads in the same process as the 
   one that created the object.
     PTHREAD_PROCESS_SHARED   The barrier object it is attached to may be 
   accessed by threads in processes other than the 
   one that created the object.
 
问题:如何在其他的进程的线程中来访问某个属性值为PTHREAD_PROCESS_SHARED   的 barrier呢???
原创粉丝点击