线程同步的方法有哪些?Linux下实现线程同步的三种方法( 三 )

< 10; i)57{58p = (struct node*)malloc(sizeof(struct node));59p->n_number = i;60pthread_mutex_lock(&mtx); //需要操作head这个临界资源,先加锁,61p->n_next = head;62head = p;63pthread_cond_signal(&cond);64pthread_mutex_unlock(&mtx); //解锁65sleep(1);66}67printf("thread 1 wanna end the line.So cancel thread 2./n");68//关于pthread_cancel,有一点额外的说明,它是从外部终止子线程,子线程会在最近的取消点,退出69//线程,而在我们的代码里,最近的取消点肯定就是pthread_cond_wait()了 。70pthread_cancel(tid);71pthread_join(tid, NULL);72printf("All done -- exiting/n");73return 0;74}复制代码#include #include #include "stdio.h"#include "stdlib.h"static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;struct node{int n_number;struct node *n_next;}*head = NULL;static void cleanup_handler(void *arg){printf("Cleanup handler of second thread./n");free(arg);(void)pthread_mutex_unlock(&mtx);}static void *thread_func(void *arg){struct node *p = NULL;pthread_cleanup_push(cleanup_handler, p);while (1){//这个mutex主要是用来保证pthread_cond_wait的并发性pthread_mutex_lock(&mtx);while (head == NULL){//这个while要特别说明一下,单个pthread_cond_wait功能很完善,为何//这里要有一个while (head == NULL)呢?因为pthread_cond_wait里的线//程可能会被意外唤醒,如果这个时候head != NULL,则不是我们想要的情况 。//这个时候,应该让线程继续进入pthread_cond_wait// pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx,//然后阻塞在等待对列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立//而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mtx);,再读取资源//用这个流程是比较清楚的pthread_cond_wait(&cond, &mtx);p = head;head = head->n_next;printf("Got %d from front of queue/n", p->n_number);free(p);}pthread_mutex_unlock(&mtx); //临界区数据操作完毕,释放互斥锁}pthread_cleanup_pop(0);return 0;}int main(void){pthread_t tid;int i;struct node *p;//子线程会一直等待资源,类似生产者和消费者,但是这里的消费者可以是多个消费者,而//不仅仅支持普通的单个消费者,这个模型虽然简单,但是很强大pthread_create(&tid, NULL, thread_func, NULL);sleep(1);for (i = 0; i < 10; i){p = (struct node*)malloc(sizeof(struct node));p->n_number = i;pthread_mutex_lock(&mtx); //需要操作head这个临界资源,先加锁,p->n_next = head;head = p;pthread_cond_signal(&cond);pthread_mutex_unlock(&mtx); //解锁sleep(1);}printf("thread 1 wanna end the line.So cancel thread 2./n");//关于pthread_cancel,有一点额外的说明,它是从外部终止子线程,子线程会在最近的取消点,退出//线程,而在我们的代码里,最近的取消点肯定就是pthread_cond_wait()了 。pthread_cancel(tid);pthread_join(tid, NULL);printf("All done -- exiting/n");return 0;}三、信号量(sem)
如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的 。信号量函数的名字都以“sem_”打头 。线程使用的基本信号量函数有四个 。
1、信号量初始化 。
int sem_init (sem_t *sem ,int pshared,unsigned int value);
这是对由sem指定的信号量进行初始化,设置好它的共享选项(linux 只支持为0,即表示它是当前进程的局部信号量),然后给它一个初始值VALUE 。
2、等待信号量 。给信号量减1,然后等待直到信号量的值大于0 。
int sem_wait(sem_t *sem);
3、释放信号量 。信号量值加1 。并通知其他等待线程 。
int sem_post(sem_t *sem);
4、销毁信号量 。我们用完信号量后都它进行清理 。归还占有的一切资源 。
int sem_destroy(sem_t *sem);
01#include 02#include 03#include 04#include 05#include 06#include 07#define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__);return;}08typedef struct _PrivInfo09{10sem_t s1;11sem_t s2;12time_t end_time;13}PrivInfo;14static void info_init (PrivInfo* thiz);15static void info_destroy (PrivInfo* thiz);16static void* pthread_func_1 (PrivInfo* thiz);17static void* pthread_func_2 (PrivInfo* thiz);18int main (int argc, char** argv)19{20pthread_t pt_1 = 0;21pthread_t pt_2 = 0;22int ret = 0;23PrivInfo* thiz = NULL;24thiz = (PrivInfo* )malloc (sizeof (PrivInfo));25if (thiz == NULL)26{27printf ("[%s]: Failed to malloc priv./n");28return -1;29}30info_init (thiz);31ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz);32if (ret != 0)33{34perror ("pthread_1_create:");35}36ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz);37if (ret != 0)38{39perror ("pthread_2_create:");40}41pthread_join (pt_1, NULL);42pthread_join (pt_2, NULL);43info_destroy (thiz);44return 0;45}46static void info_init (PrivInfo* thiz)47{48return_if_fail (thiz != NULL);49thiz->end_time = time(NULL)10;50sem_init (&thiz->s1, 0, 1);51sem_init (&thiz->s2, 0, 0);52return;53}54static void info_destroy (PrivInfo* thiz)55{56return_if_fail (thiz != NULL);57sem_destroy (&thiz->s1);58sem_destroy (&thiz->s2);59free (thiz);60thiz = NULL;61return;62}63static void* pthread_func_1 (PrivInfo* thiz)64{65return_if_fail(thiz != NULL);66while (time(NULL)

推荐阅读