多线程互斥锁问题(多线程模拟银行存取款)
2012-03-12 19:39
232 查看
通过以下代码可以看到互斥锁的重要性:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
static int totalmoney = 20000; //总金额20000
pthread_mutex_t mutex ; //定义变量
int Check_balance()
{
return totalmoney;
}
void Save_money(int samoney)
{
int surplus;
while(1)
{
//pthread_mutex_lock( &mutex ) ; //@a1,锁定mutex指向的互斥锁
surplus = Check_balance();
sleep(1);
totalmoney = surplus + samoney;
printf("After save money current balance is %d\n",totalmoney);
//pthread_mutex_unlock( &mutex ) ; //@a1,给mutex指向的互斥锁解锁
sleep(1);
}
}
void Draw_money(int gemoney)
{
while(1)
{
//pthread_mutex_lock( &mutex ) ; //@a2,锁定mutex指向的互斥锁
totalmoney = totalmoney - gemoney;
printf("After draw money current balance is %d\n",totalmoney);
sleep(1);
//pthread_mutex_unlock( &mutex ) ; //@a2,给mutex指向的互斥锁解锁
}
}
int main()
{
pthread_t thread1,thread2;
int ret1,ret2;
int samoney = 5000; //存款金额
int gemoney = 10000; //取款金额
pthread_mutex_init( &mutex , NULL ) ; //初始化互斥锁
printf("Total money in the account is %d\n\n",totalmoney);
ret1 = pthread_create(&thread1,NULL,(void *)Save_money,(void *)samoney);
if(ret1)
{
fprintf(stdout,"Create thread failed!\n");
return -1;
}
sleep(2);
ret2 = pthread_create(&thread2,NULL,(void *)Draw_money,(void *)gemoney);
if(ret2)
{
fprintf(stdout,"Create thread failed!\n");
return -1;
}
sleep(4);
return 0;
}
取消@a1、@a2处的互斥锁(即不使用互斥锁)时其中又一次的结果如下:
Total money in the account is 20000
After save money current balance is 25000
After draw money current balance is 15000
After save money current balance is 20000
After draw money current balance is 10000
After draw money current balance is 0
After save money current balance is 15000
After draw money current balance is 5000
Total money in the account is 5000
总共存了3次(共15000),取了4次(共40000),这样就只赚(赚了10000)不赔了,要是银行都这样就好了,呵呵~~~
在@a1、@a2处加上互斥锁(即使用互斥锁)时其中又一次的结果如下:
Total money in the account is 20000
After save money current balance is 25000
After draw money current balance is 15000
After save money current balance is 20000
After draw money current balance is 10000
Total money in the account is 10000
总共存了2次(共10000),取了2次(共20000),这样就谁都不会亏了~~~
互斥锁解决了线程不同步问题
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
static int totalmoney = 20000; //总金额20000
pthread_mutex_t mutex ; //定义变量
int Check_balance()
{
return totalmoney;
}
void Save_money(int samoney)
{
int surplus;
while(1)
{
//pthread_mutex_lock( &mutex ) ; //@a1,锁定mutex指向的互斥锁
surplus = Check_balance();
sleep(1);
totalmoney = surplus + samoney;
printf("After save money current balance is %d\n",totalmoney);
//pthread_mutex_unlock( &mutex ) ; //@a1,给mutex指向的互斥锁解锁
sleep(1);
}
}
void Draw_money(int gemoney)
{
while(1)
{
//pthread_mutex_lock( &mutex ) ; //@a2,锁定mutex指向的互斥锁
totalmoney = totalmoney - gemoney;
printf("After draw money current balance is %d\n",totalmoney);
sleep(1);
//pthread_mutex_unlock( &mutex ) ; //@a2,给mutex指向的互斥锁解锁
}
}
int main()
{
pthread_t thread1,thread2;
int ret1,ret2;
int samoney = 5000; //存款金额
int gemoney = 10000; //取款金额
pthread_mutex_init( &mutex , NULL ) ; //初始化互斥锁
printf("Total money in the account is %d\n\n",totalmoney);
ret1 = pthread_create(&thread1,NULL,(void *)Save_money,(void *)samoney);
if(ret1)
{
fprintf(stdout,"Create thread failed!\n");
return -1;
}
sleep(2);
ret2 = pthread_create(&thread2,NULL,(void *)Draw_money,(void *)gemoney);
if(ret2)
{
fprintf(stdout,"Create thread failed!\n");
return -1;
}
sleep(4);
return 0;
}
取消@a1、@a2处的互斥锁(即不使用互斥锁)时其中又一次的结果如下:
Total money in the account is 20000
After save money current balance is 25000
After draw money current balance is 15000
After save money current balance is 20000
After draw money current balance is 10000
After draw money current balance is 0
After save money current balance is 15000
After draw money current balance is 5000
Total money in the account is 5000
总共存了3次(共15000),取了4次(共40000),这样就只赚(赚了10000)不赔了,要是银行都这样就好了,呵呵~~~
在@a1、@a2处加上互斥锁(即使用互斥锁)时其中又一次的结果如下:
Total money in the account is 20000
After save money current balance is 25000
After draw money current balance is 15000
After save money current balance is 20000
After draw money current balance is 10000
Total money in the account is 10000
总共存了2次(共10000),取了2次(共20000),这样就谁都不会亏了~~~
互斥锁解决了线程不同步问题
相关文章推荐
- 多线程:利用互斥锁来处理全局变量的互斥问题
- 多线程_互斥锁解决买票问题
- C++11实现多线程 互斥锁与资源竞争问题
- 一个折腾很久的支付宝支付的问题【多线程问题】
- Java多线程里总线锁定和缓存一致性的问题
- 多线程后台语音播放系统,支持多声卡的问题
- 关于多线程-银行出纳问题
- 【python】多线程queue导致的死锁问题
- 集群中多线程如何处理表中的数据的问题
- Python基于多线程操作数据库相关问题分析
- 多线程互斥-读写者问题
- Java程序员面试中的多线程问题
- 多线程下的内存释放问题
- 关于linux中多线程编程的一些初级问题
- 在多线程中使用静态方法是否有线程安全问题
- MFC多线程调用UpdateData函数问题
- Servlet配置对象、上下文对象、多线程问题
- Java Note: 多线程的同步(互斥锁)的方法对比,信号量锁,读写锁的实现,生产者-消费者模式的实现
- java多线程_并发问题的解析
- Java多线程的同步问题