[Linux] 黄牛抢票系统(互斥锁实现互斥)

version 1.0

4 个黄牛,每个黄牛相当于一个线程

/*************************************************************************
*> version 1.0
*> Author: giturtle
*> Describe: 实现互斥锁的基本使用以及线程安全的基本认识
    黄牛抢票:共100张票,共有 4 个黄牛在争抢票
*************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

int ticket = 100;	//全局变量

pthread_mutex_t mutex;

void *thr_start(void *arg){
    while(1) {
    	//加锁要在临界资源访问之前,所以在抢到票操作之前进行加锁
		pthread_mutex_lock(&mutex);
    	//一定要在while内加锁,如果在循环外加锁,且在循环内解锁,那么就不会再加锁了
		if (ticket > 0) {		//有票,黄牛进行抢票操作
		    usleep(1000);
		    printf("yellow bull[%d]:get a ticket:No.[%d]\n", (int)arg, ticket);	//用参数arg传递黄牛的id
		    ticket--;
		}else {
		    //加锁之后需要在线程任意有可能退出的地方进行解锁
		    pthread_mutex_unlock(&mutex);	//在任何线程有可能退出的地方都要进行解锁操作
		    pthread_exit(NULL);
		}
		pthread_mutex_unlock(&mutex);	//解锁
	}
    return NULL;
}
int main(int argc, char *argv[]){
    pthread_t tid[4];	//4 个黄牛,每个黄牛相当于一个线程
    int i = 0, ret;

    pthread_mutex_init(&mutex, NULL);
    for (; i < 4; i++) {
		ret = pthread_create(&tid[i], NULL, thr_start, (void*)i);
		if (ret != 0) {
		    printf("yellow bull no exist!!\n");
		    return -1;
		}
    }
    for (i = 0; i < 4; i++) {
		pthread_join(tid[i], NULL);
    }
    pthread_mutex_destroy(&mutex);
    return 0;
}

运行结果

yellow bull[0]get a ticket:No.[100]
yellow bull[0]get a ticket:No.[99]
yellow bull[0]get a ticket:No.[98]
yellow bull[0]get a ticket:No.[97]
yellow bull[0]get a ticket:No.[96]
yellow bull[0]get a ticket:No.[95]
yellow bull[0]get a ticket:No.[94]
yellow bull[0]get a ticket:No.[93]
yellow bull[0]get a ticket:No.[92]
yellow bull[0]get a ticket:No.[91]
yellow bull[0]get a ticket:No.[90]
yellow bull[0]get a ticket:No.[89]
yellow bull[0]get a ticket:No.[88]
yellow bull[0]get a ticket:No.[87]
yellow bull[0]get a ticket:No.[86]
yellow bull[0]get a ticket:No.[85]
yellow bull[0]get a ticket:No.[84]
yellow bull[0]get a ticket:No.[83]
yellow bull[0]get a ticket:No.[82]
yellow bull[0]get a ticket:No.[81]
yellow bull[0]get a ticket:No.[80]
yellow bull[0]get a ticket:No.[79]
yellow bull[0]get a ticket:No.[78]
yellow bull[0]get a ticket:No.[77]
yellow bull[0]get a ticket:No.[76]
yellow bull[0]get a ticket:No.[75]
yellow bull[0]get a ticket:No.[74]
yellow bull[0]get a ticket:No.[73]
yellow bull[0]get a ticket:No.[72]
yellow bull[0]get a ticket:No.[71]
yellow bull[0]get a ticket:No.[70]
yellow bull[0]get a ticket:No.[69]
yellow bull[0]get a ticket:No.[68]
yellow bull[0]get a ticket:No.[67]
yellow bull[0]get a ticket:No.[66]
yellow bull[0]get a ticket:No.[65]
yellow bull[0]get a ticket:No.[64]
yellow bull[0]get a ticket:No.[63]
yellow bull[0]get a ticket:No.[62]
yellow bull[0]get a ticket:No.[61]
yellow bull[0]get a ticket:No.[60]
yellow bull[0]get a ticket:No.[59]
yellow bull[0]get a ticket:No.[58]
yellow bull[0]get a ticket:No.[57]
yellow bull[0]get a ticket:No.[56]
yellow bull[0]get a ticket:No.[55]
yellow bull[0]get a ticket:No.[54]
yellow bull[0]get a ticket:No.[53]
yellow bull[0]get a ticket:No.[52]
yellow bull[0]get a ticket:No.[51]
yellow bull[0]get a ticket:No.[50]
yellow bull[0]get a ticket:No.[49]
yellow bull[0]get a ticket:No.[48]
yellow bull[0]get a ticket:No.[47]
yellow bull[0]get a ticket:No.[46]
yellow bull[0]get a ticket:No.[45]
yellow bull[0]get a ticket:No.[44]
yellow bull[0]get a ticket:No.[43]
yellow bull[0]get a ticket:No.[42]
yellow bull[0]get a ticket:No.[41]
yellow bull[0]get a ticket:No.[40]
yellow bull[0]get a ticket:No.[39]
yellow bull[0]get a ticket:No.[38]
yellow bull[0]get a ticket:No.[37]
yellow bull[0]get a ticket:No.[36]
yellow bull[0]get a ticket:No.[35]
yellow bull[3]get a ticket:No.[34]
yellow bull[3]get a ticket:No.[33]
yellow bull[3]get a ticket:No.[32]
yellow bull[3]get a ticket:No.[31]
yellow bull[3]get a ticket:No.[30]
yellow bull[3]get a ticket:No.[29]
yellow bull[3]get a ticket:No.[28]
yellow bull[3]get a ticket:No.[27]
yellow bull[3]get a ticket:No.[26]
yellow bull[3]get a ticket:No.[25]
yellow bull[3]get a ticket:No.[24]
yellow bull[3]get a ticket:No.[23]
yellow bull[3]get a ticket:No.[22]
yellow bull[3]get a ticket:No.[21]
yellow bull[3]get a ticket:No.[20]
yellow bull[3]get a ticket:No.[19]
yellow bull[3]get a ticket:No.[18]
yellow bull[3]get a ticket:No.[17]
yellow bull[3]get a ticket:No.[16]
yellow bull[3]get a ticket:No.[15]
yellow bull[3]get a ticket:No.[14]
yellow bull[3]get a ticket:No.[13]
yellow bull[3]get a ticket:No.[12]
yellow bull[3]get a ticket:No.[11]
yellow bull[3]get a ticket:No.[10]
yellow bull[3]get a ticket:No.[9]
yellow bull[3]get a ticket:No.[8]
yellow bull[3]get a ticket:No.[7]
yellow bull[3]get a ticket:No.[6]
yellow bull[3]get a ticket:No.[5]
yellow bull[3]get a ticket:No.[4]
yellow bull[3]get a ticket:No.[3]
yellow bull[3]get a ticket:No.[2]
yellow bull[3]get a ticket:No.[1]

互斥锁只保证安全(互斥),但不保证合理(同步)

我们可以看到,通过使用互斥锁完成了每一张票的唯一取得性,即黄牛们不会抢到同一张票。但这只是实现了互斥(安全性),没有实现同步(合理性):也就是几位黄牛没有做到同时抢票,即时序可控性(只有0号与3号抢到了),这是不合理的,需要进一步优化。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

giturtle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值