linux线程互斥与同步

56 篇文章 2 订阅

害羞线程互斥,解决多个线程访问同一资源时的竞态问题:

/*************************************************************************
	> File Name: mutex.c
	> Author: XXDK
	> Email: v.manstein@qq.com 
	> Created Time: Wed 15 Mar 2017 11:18:10 PM PDT
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
#include<string.h>


typedef struct message {
	char buf[10];
	pthread_mutex_t lock;
}mesg_t;

void *print_func(void *args)
{
	mesg_t* mesgp = (mesg_t*)args;

	while(1) {
		pthread_mutex_lock(&mesgp->lock);
		printf("%s\n", mesgp->buf);
		pthread_mutex_unlock(&mesgp->lock);
	}
}

void *revrs_func(void *args)
{
	int len;
	char temp;

	mesg_t* mesgp = (mesg_t*)args;
	len = strlen(mesgp->buf);		
	while(1) {
		pthread_mutex_lock(&mesgp->lock); // 临界区
			for(int i = 0; i <= len/2; i++) {
				temp = mesgp->buf[i]; 
				mesgp->buf[i] = mesgp->buf[len - i - 1];
				mesgp->buf[len - i - 1] = temp;
			}
		printf("reverse ok\n");
		pthread_mutex_unlock(&mesgp->lock);
	}
}


int main()
{
	pthread_t tid[2];
	mesg_t mesg = {"ABCDEFGHI", 0};
		
	pthread_mutex_init(&mesg.lock, NULL);

	pthread_create(&tid[0], NULL, print_func, &mesg);
	pthread_create(&tid[1], NULL, revrs_func, &mesg);

	pause();

	exit(1);
}

生气线程同步-条件变量,解决多线程按照需求的顺序协同运行的问题:

/*************************************************************************
	> File Name: cond.c
	> Author: XXDK
	> Email: v.manstein@qq.com 
	> Created Time: Thu 16 Mar 2017 12:41:42 AM PDT
 ************************************************************************/

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

volatile int flag = 0;

pthread_mutex_t mlock;
pthread_cond_t cond;

void *hello(void *arg)
{
	while(1) {
		pthread_mutex_lock(&mlock);
		while(flag != 0) {
			// 1. 自动释放mutex,线程阻塞条件变量
			// 2. 睡眠等待条件满足,由条件广播函数唤醒
			pthread_cond_wait(&cond, &mlock);
		}
		flag = 1;
		printf("hello\n");

		pthread_mutex_unlock(&mlock);
		pthread_cond_broadcast(&cond);
	}
}
void *the(void *arg)
{
	while(1) {
		pthread_mutex_lock(&mlock);
		while(flag != 1) {
			pthread_cond_wait(&cond, &mlock);
		}
		flag = 2;
		printf("the\n");
		pthread_mutex_unlock(&mlock);
		pthread_cond_broadcast(&cond);
	}
}
void *world(void *arg)
{
	while(1) {
		pthread_mutex_lock(&mlock);
		while(flag != 2) {
			pthread_cond_wait(&cond, &mlock);
		}
		flag = 0;
		printf("world\n");
		pthread_mutex_unlock(&mlock);
		pthread_cond_broadcast(&cond);
	}
}

int main()
{
	pthread_t tid[3];

	pthread_mutex_init(&mlock, NULL);
	pthread_cond_init(&cond, NULL);

	pthread_create(&tid[0], NULL, hello, NULL);
	pthread_create(&tid[1], NULL, the  , NULL);
	pthread_create(&tid[2], NULL, world, NULL);

	pause();

	exit(1);
}
生气线程同步-信号量,解决多线程按照需求的顺序协同运行的问题:

/*************************************************************************
	> File Name: sema.c
	> Author: XXDK
	> Email: v.manstein@qq.com 
	> Created Time: Thu 16 Mar 2017 08:00:00 PM PDT
 ************************************************************************/

#include<stdio.h>
#include<semaphore.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>

sem_t sem;
void* first(void*args)
{
	sem_wait(&sem); // 等待信号量可用
	printf("12345\n");
}

void* second(void*args)
{
	printf("67890\n");
	sem_post(&sem); // 信号量+1
}
int main()
{
	pthread_t tid[2];

	sem_init(&sem, 0, 0); // 设定信号量初值为0
	pthread_create(&tid[0], NULL, first, NULL);
	pthread_create(&tid[1], NULL, second, NULL);
	
	sleep(1);

	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值