APUE學習筆記-11.6線程同步

(1)不存在一致性問題的狀況
算法

    1)每一個線程使用的變量都是其餘線程不會使用和修改的。
數據結構

    2)變量是隻讀的。
dom

    3)對變量的修改操做時原子操做,則不存在競爭。
函數

    4)數據老是順序一致出現的。(有計算機體系結構決定)spa


<322>使用互斥量保護數據結構線程

#include <stdlib.h>
#include <pthread.h>

struct foo {
	int             f_count;
	pthread_mutex_t f_lock;
	int             f_id;
	/* ... more stuff here ... */
};

struct foo *
foo_alloc(int id) /* allocate the object */
{
	struct foo *fp;

	if ((fp = malloc(sizeof(struct foo))) != NULL) {
		fp->f_count = 1;
		fp->f_id = id;
		if (pthread_mutex_init(&fp->f_lock, NULL) != 0) {
			free(fp);
			return(NULL);
		}
		/* ... continue initialization ... */
	}
	return(fp);
}

void
foo_hold(struct foo *fp) /* add a reference to the object */
{
	pthread_mutex_lock(&fp->f_lock);
	fp->f_count++;
	pthread_mutex_unlock(&fp->f_lock);
}

void
foo_rele(struct foo *fp) /* release a reference to the object */
{
	pthread_mutex_lock(&fp->f_lock);
	if (--fp->f_count == 0) { /* last reference */
		pthread_mutex_unlock(&fp->f_lock);
		pthread_mutex_destroy(&fp->f_lock);
		free(fp);
	} else {
		pthread_mutex_unlock(&fp->f_lock);
	}
}

(1)rest

int  pthread_mutex_init(pthread_mutex_t  *mutex,  const  pthread_mutexattr_t *mutexattr);code

mutexattr:NULL表示用默認的屬性初始化互斥量。orm


int pthread_mutex_lock(pthread_mutex_t *mutex);對象

對互斥量進行加鎖,若互斥量已經上鎖,則調用線程將阻塞直到互斥量被解鎖。


int pthread_mutex_trylock(pthread_mutex_t *mutex);

嘗試對互斥量進行加鎖,有2種結果:

    鎖住互斥量,直接返回0。

    失敗,返回EBUSY。


int pthread_mutex_unlock(pthread_mutex_t *mutex);

對互斥量解鎖。


int pthread_mutex_destroy(pthread_mutex_t *mutex);

動態分配的互斥量,(經過調用malloc函數)在釋放內存前需調用此函數。


<337>在一個任務上合做的多個線程之間如何用屏障進行同步

這個例子中,使用8個線程分解了800萬個數的排序工做。每一個線程用堆排序算法對100萬個數進行排序。最後主線程調用一個函數對這些結果進行合併。

#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);
}

(1)

屏障用於協調多個線程並行工做。

它們容許任意數量的線程等待,直到全部的線程完成處理工做,而不須要退出。全部線程到達屏障後能夠接着工做。

(2)

srandom函數:設定隨機數種子。

random函數:產生隨機數。

(3)

int pthread_barrier_init(pthread_barrier_t *restrict barrier

              const pthread_barrierattr_t *restrict attr, unsigned count);

初始化屏障。
count參數:在容許全部線程繼續運行以前,必須到達屏障的線程數目。

attr參數:指定屏障對象的屬性。NULL表示用默認屬性初始化屏障。


int pthread_barrier_wait(pthread_barrier_t *barrier);

調用此函數的線程在屏障未知足條件時,會進入休眠狀態。若該線程是最後一個調用此函數的線程,知足屏障計數,則全部的線程都被喚醒。

相關文章
相關標籤/搜索