Linux线程3种同步方法

一、互斥锁(mutex)

通过锁机制实现线程间的同步。

  1. 初始化锁。在Linux下,线程的互斥量数据类型是pthread_mutex_t。在使用前,要对它进行初始化。
    静态分配:pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    动态分配:int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutex_attr_t *mutexattr);
  2. 加锁。对共享资源的访问,要对互斥量进行加锁,如果互斥量已经上了锁,调用线程会阻塞,直到互斥量被解锁。
    int pthread_mutex_lock(pthread_mutex *mutex);
    int pthread_mutex_trylock(pthread_mutex_t *mutex);
  3. 解锁。在完成了对共享资源的访问后,要对互斥量进行解锁。
    int pthread_mutex_unlock(pthread_mutex_t *mutex);
  4. 销毁锁。锁在是使用完成后,需要进行销毁以释放资源。
    int pthread_mutex_destroy(pthread_mutex *mutex);
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<semaphore.h>

/*
 * 实现i++功能
 * 
 * */
sem_t sem;
int i=0;
void couta(){
	while(1){
		sem_wait(&sem);
		i++;
		printf("%d ",i);
		sem_post(&sem);
	}
}

void coutb(){
	while(1){
		sem_wait(&sem);
		i++;
		printf("%d ",i);
		sem_post(&sem);
	}
}

void init(){
		sem_init(&sem,0,1);
}
int main(int argc,char**argv){
	pthread_t ta,tb;
	init();
	pthread_create(&ta,NULL,couta,0);
	pthread_create(&tb,NULL,coutb,0);
	pthread_join(ta,NULL);
	pthread_join(tb,NULL);
	sem_destroy(&sem);
	return 0;
}

二、条件变量(cond)

互斥锁不同,条件变量是用来等待而不是用来上锁的。条件变量用来自动阻塞一个线程,直到某特殊情况发生为止。通常条件变量和互斥锁同时使用。条件变量分为两部分: 条件和变量。条件本身是由互斥量保护的。线程在改变条件状态前先要锁住互斥量。条件变量使我们可以睡眠等待某种条件出现。条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号)。条件的检测是在互斥锁的保护下进行的。如果一个条件为假,一个线程自动阻塞,并释放等待状态改变的互斥锁。如果另一个线程改变了条件,它发信号给关联的条件变量,唤醒一个或多个等待它的线程,重新获得互斥锁,重新评价条件。如果两进程共享可读写的内存,条件变量可以被用来实现这两进程间的线程同步。

  1. 初始化条件变量。
    静态态初始化,pthread_cond_t cond = PTHREAD_COND_INITIALIER;
    动态初始化,int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
  2. 等待条件成立。释放锁,同时阻塞等待条件变量为真才行。timewait()设置等待时间,仍未signal,返回ETIMEOUT(加锁保证只有一个线程wait)
    int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
    int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);
  3. 激活条件变量。pthread_cond_signal,pthread_cond_broadcast(激活所有等待线程)
    int pthread_cond_signal(pthread_cond_t *cond);
    int pthread_cond_broadcast(pthread_cond_t *cond); //解除所有线程的阻塞
  4. 清除条件变量。无线程等待,否则返回EBUSY
    int pthread_cond_destroy(pthread_cond_t *cond);
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<time.h>
#include"pthread.h"

/*
 *
 * 输出a b a b a b ... 
 */

pthread_cond_t aout;
pthread_cond_t bout;
pthread_mutex_t lock;
void init(){
		pthread_mutex_init(&lock,NULL);
		pthread_cond_init(&aout,NULL);
		pthread_cond_init(&bout,NULL);
}

void couta(){
	while(1){
		pthread_mutex_lock(&lock);
		printf("a ");
		pthread_cond_signal(&bout);
		pthread_cond_wait(&aout,&lock);
		pthread_mutex_unlock(&lock);
	}
}

void coutb(){
	while(1){
		pthread_mutex_lock(&lock);
		printf("b ");
		pthread_cond_signal(&aout);
		pthread_cond_wait(&bout,&lock);
		pthread_mutex_unlock(&lock);
	}
}
int main(int argc,char**argv){
	init();
	pthread_t ta,tb;
	pthread_create(&ta,NULL,couta,0);
	pthread_create(&tb,NULL,coutb,0);
	pthread_join(ta,NULL);
	pthread_join(tb,NULL);
	pthread_mutex_destroy(&lock);
	pthread_cond_destroy(&aout);
	pthread_cond_destroy(&bout);
	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);
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<semaphore.h>

/*
 * 实现i++功能
 * 
 * */
sem_t sem;
int i=0;
void couta(){
	while(1){
		sem_wait(&sem);
		i++;
		printf("%d ",i);
		sem_post(&sem);
	}
}

void coutb(){
	while(1){
		sem_wait(&sem);
		i++;
		printf("%d ",i);
		sem_post(&sem);
	}
}

void init(){
		sem_init(&sem,0,1);
}
int main(int argc,char**argv){
	pthread_t ta,tb;
	init();
	pthread_create(&ta,NULL,couta,0);
	pthread_create(&tb,NULL,coutb,0);
	pthread_join(ta,NULL);
	pthread_join(tb,NULL);
	sem_destroy(&sem);
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值