屏障給我們提供了多個線程協調工作的一種方式,屏障允許每個線程等待,直到所有的合作線程都到達某一點,然後從該點繼續執行。有了屏障,我們處理合作線程就變得簡單多了。
下面來介紹一下相關的函數
和前面的線程同步的函數類似,init用來初始化,destroy用來反向初始化。
初始化函數 pthread_barrier_init的第三個參數count可以用來指定必須到達屏障的線程數目。
pthread_barrier_wait()函數表明線程已經完成了它的工作,等待其它線程會和。調用該函數的線程在屏障計數沒有滿的時候,會進入休眠狀態。當最後一個線程調用該函數的時候,那麼就會喚醒前面進入休眠狀態的線程(因爲此時就滿足了屏障計數)。
一旦達到屏障計數,而且線程處於非阻塞狀態,屏障就可以被重用。如果想重設屏障計數,那就需要調用destroy函數,然後調用init函數重新設置。否則屏障計數不會改變。
下面,我們就來體驗一下多線程的厲害之處。
#include "apue.h"
#include <pthread.h>
#include <limits.h>
#include <sys/time.h>
#define NTHR 8 /* number of threads */
#define NUMNUM 8000000L /* number of numbers to sort */
#define TNUM (NUMNUM/NTHR) /* number to sort per thread */
long nums[NUMNUM];
long snums[NUMNUM];
pthread_barrier_t b;
#ifdef SOLARIS
#define heapsort qsort
#else
extern int heapsort(void *, size_t, size_t,
int (*)(const void *, const void *));
#endif
/*
* Compare two long integers (helper function for heapsort)
*/
int
complong(const void *arg1, const void *arg2)
{
long l1 = *(long *)arg1;
long l2 = *(long *)arg2;
if (l1 == l2)
return 0;
else if (l1 < l2)
return -1;
else
return 1;
}
/*
* Worker thread to sort a portion of the set of numbers.
*/
void *
thr_fn(void *arg)
{
long idx = (long)arg;
heapsort(&nums[idx], TNUM, sizeof(long), complong);
pthread_barrier_wait(&b);
/*
* Go off and perform more work ...
*/
return((void *)0);
}
/*
* Merge the results of the individual sorted ranges.
*/
void
merge()
{
long idx[NTHR];
long i, minidx, sidx, num;
for (i = 0; i < NTHR; i++)
idx[i] = i * TNUM;
for (sidx = 0; sidx < NUMNUM; sidx++) {
num = LONG_MAX;
for (i = 0; i < NTHR; i++) {
if ((idx[i] < (i+1)*TNUM) && (nums[idx[i]] < num)) {
num = nums[idx[i]];
minidx = i;
}
}
snums[sidx] = nums[idx[minidx]];
idx[minidx]++;
}
}
int
main()
{
unsigned long i;
struct timeval start, end;
long long startusec, endusec;
double elapsed;
int err;
pthread_t tid;
/*
* Create the initial set of numbers to sort.
*/
srandom(1);
for (i = 0; i < NUMNUM; i++)
nums[i] = random();
/*
* Create 8 threads to sort the numbers.
*/
gettimeofday(&start, NULL);
pthread_barrier_init(&b, NULL, NTHR+1);
for (i = 0; i < NTHR; i++) {
err = pthread_create(&tid, NULL, thr_fn, (void *)(i * TNUM));
if (err != 0)
err_exit(err, "can't create thread");
}
pthread_barrier_wait(&b);
merge();
gettimeofday(&end, NULL);
/*
* Print the sorted list.
*/
startusec = start.tv_sec * 1000000 + start.tv_usec;
endusec = end.tv_sec * 1000000 + end.tv_usec;
elapsed = (double)(endusec - startusec) / 1000000.0;
printf("sort took %.4f seconds\n", elapsed);
/*for (i = 0; i < NUMNUM; i++)
printf("%ld\n", snums[i]);
*/
exit(0);
}
在我的這個電腦上可以得到的運行結果如下: