Linux同步4 ——barrier linux系统时间同步

barrier用于多个线程同步某个任务的完成状态,比如循环处理数组的多线程程序并行。
barrier定义:typedef union{char__size[__SIZEOF_PTHREAD_BARRIER_T];long int __align;} pthread_barrier_t;
struct pthread_barrier{unsigned intcurr_event;//等待Count用变量int lock;unsigned intleft;//Down Counterunsigned intinit_count;//Counter初始值};
Linux同步(4)——barrier linux系统时间同步
barrier初始化:intpthread_barrier_init (barrier, attr, count) pthread_barrier_t *barrier; constpthread_barrierattr_t *attr; unsigned int count;{struct pthread_barrier*ibarrier;
if (__builtin_expect(count == 0, 0)) returnEINVAL;
if (attr != NULL) { struct pthread_barrierattr *iattr;
iattr = (struct pthread_barrierattr *)attr;
if (iattr->pshared !=PTHREAD_PROCESS_PRIVATE &&__builtin_expect (iattr->pshared !=PTHREAD_PROCESS_SHARED, 0)) return EINVAL; }
ibarrier = (structpthread_barrier *) barrier;
ibarrier->lock =LLL_LOCK_INITIALIZER;ibarrier->left =count;ibarrier->init_count= count;ibarrier->curr_event= 0;
return 0;}
barrier等待:intpthread_barrier_wait (barrier) pthread_barrier_t *barrier;{struct pthread_barrier*ibarrier = (struct pthread_barrier *) barrier;int result = 0;
lll_lock(ibarrier->lock);--ibarrier->left;
//这种情况下,说明已经达到了指定的等待次数//唤醒所有的等待线程//虽然对最后一个调用该函数的线程返回PTHREAD_BARRIER_SERIAL_THREAD//但是POSIX规定这是 不 定 的if(ibarrier->left == 0) { ++ibarrier->curr_event;
lll_futex_wake(&ibarrier->curr_event,INT_MAX);
result = PTHREAD_BARRIER_SERIAL_THREAD; } else { unsigned int event =ibarrier->curr_event;
lll_unlock (ibarrier->lock);
do lll_futex_wait(&ibarrier->curr_event,event); w hile (event ==ibarrier->curr_event); }
unsigned int init_count =ibarrier->init_count;
if(atomic_increment_val(&ibarrier->left) ==init_count) lll_unlock (ibarrier->lock);
returnresult;}
API函数:int pthread_barrier_init(pthread_barrier_t *barrier, constpthread_barrierattr_t *attr, unsigned int count);int pthread_barrier_wait(thread_barrier_t *barrier);int pthread_barrier_destroy(ed_barrier_t *barrier);int pthread_barrierattr_init(pthread_barrierattr_t*attr);int pthread_barrierattr_setpshared(pthread_barrierattr_t*attr, int pshared);int pthread_barrierattr_getpshared(const pthread_barrierattr_t*attr, int *pshared);

  

爱华网本文地址 » http://www.413yy.cn/a/25101015/265202.html

更多阅读

如何在linux系统中设置静态ip地址 linux系统设置ip地址

如何在linux系统中设置静态ip地址——简介本篇主要是介绍一下在linux系统中设置静态ip地址方法。如何在linux系统中设置静态ip地址——工具/原料redhat6.4 x64如何在linux系统中设置静态ip地址——方法/步骤如何在linux系统中设置

linux系统改装win7 新机如何装win7系统

linux系统改装win7——简介很多新买的电脑都没有预装win系统,出厂厂家为了节约成本,一般预装了linux或dos,但是这个linux一般是国产的红旗linux,对于一般的用户根本没有任何可以使用的价值,连播放个视频可能就得捣鼓半天。所以

声明:《Linux同步4 ——barrier linux系统时间同步》为网友莪聴聞分享!如侵犯到您的合法权益请联系我们删除