多线程的学习,执行情况和显示的效果不一致,求教

小哥该吃药了 2014-10-15 03:45:30
今天在学习多线程编程时,在vs2010上运行了孙鑫老师深入详解vc里的一段代码。
发现win32控制台显示的结果很奇怪,多线程调用正常,但是cout输出滞后了。以下贴出代码
#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;
DWORD WINAPI Fun1Proc(LPVOID lpParameter);
DWORD WINAPI Fun2Proc(LPVOID lpParameter);
int t=0;
int tickets=100;
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread1;
HANDLE hThread2;

hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
while (t++<1000)
Sleep(4000);
system("pause");
return 0;
}

DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
while(TRUE)
{
if(tickets>0)
cout<<"thread1 sell ticket :"<<tickets--<<endl;
else
break;
}
return 0;
}
DWORD WINAPI Fun2Proc(LPVOID lpParameter)
{
while(TRUE)
{
if(tickets>0)
cout<<"thread2 sell ticket :"<<tickets--<<endl;
else
break;
}
return 0;
}



以下是执行结果:
thread1 sell ticket :thread2 sell ticket :99
100
thread1 sell ticket :98
thread1 sell ticket :97
thread2 sell ticket :96
thread1 sell ticket :95
thread2 sell ticket :94
thread2 sell ticket :92thread1 sell ticket :93
thread1 sell ticket :91

thread1 sell ticket :90
thread2 sell ticket :89
thread2 sell ticket :87
thread2 sell ticket :86
thread2 sell ticket :85
thread2 sell ticket :84
thread2 sell ticket :83
thread1 sell ticket :88
thread1 sell ticket :81
thread1 sell ticket :80
thread1 sell ticket :79
thread2 sell ticket :82
thread1 sell ticket :78
thread2 sell ticket :77
thread1 sell ticket :76
thread1 sell ticket :74
thread1 sell ticket :73
thread1 sell ticket :72
thread1 sell ticket :71
thread1 sell ticket :70
thread1 sell ticket :69
thread1 sell ticket :68
thread1 sell ticket :67
thread1 sell ticket :66
thread1 sell ticket :65
thread1 sell ticket :64
thread1 sell ticket :63
thread1 sell ticket :62
thread1 sell ticket :61
thread1 sell ticket :60
thread1 sell ticket :59
thread1 sell ticket :58
thread1 sell ticket :57
thread1 sell ticket :56
thread1 sell ticket :55
thread1 sell ticket :54
thread1 sell ticket :53
thread1 sell ticket :52
thread1 sell ticket :51
thread1 sell ticket :50
thread1 sell ticket :49
thread1 sell ticket :48
thread1 sell ticket :47
thread1 sell ticket :46
thread1 sell ticket :45
thread1 sell ticket :44
thread1 sell ticket :43
thread1 sell ticket :42
thread1 sell ticket :41
thread1 sell ticket :40
thread1 sell ticket :39
thread1 sell ticket :38
thread1 sell ticket :37
thread1 sell ticket :36
thread1 sell ticket :35
thread1 sell ticket :34
thread2 sell ticket :75
thread2 sell ticket :33
thread2 sell ticket :32
thread2 sell ticket :31
thread2 sell ticket :30
thread2 sell ticket :29
thread2 sell ticket :28
thread2 sell ticket :27
thread2 sell ticket :26
thread2 sell ticket :25
thread2 sell ticket :24
thread2 sell ticket :23
thread2 sell ticket :22
thread2 sell ticket :21
thread2 sell ticket :20
thread2 sell ticket :19
thread2 sell ticket :18
thread2 sell ticket :17
thread2 sell ticket :16
thread2 sell ticket :15
thread2 sell ticket :14
thread2 sell ticket :13
thread2 sell ticket :12
thread2 sell ticket :11
thread2 sell ticket :10
thread2 sell ticket :9
thread2 sell ticket :8
thread2 sell ticket :7
thread2 sell ticket :6
thread2 sell ticket :5
thread2 sell ticket :4
thread2 sell ticket :3
thread2 sell ticket :2
thread2 sell ticket :1
————————————————————————————————————
问题来了,93号那张票,既然线程1抢到了,为何不是先输出,而是92号那张票先输出。同样这里面还有82,88号票等,每次运行都有几个这样的情况。
...全文
87 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
小哥该吃药了 2014-10-15
  • 打赏
  • 举报
回复
引用 4 楼 shenyi0106 的回复:
多线程的一个非常重要的概念就是——锁!!! 不理解锁,就别搞多线程了,这只会使你的思想更混乱
我刚刚接触,还没看到后面的概念。跟着孙鑫老师的步骤走,发现跟他的结果不一样,又不明白为什么所以贴出来问。
lx624909677 2014-10-15
  • 打赏
  • 举报
回复
引用 2 楼 yang2009001 的回复:
[quote=引用 1 楼 lx624909677 的回复:] 给票数加锁,多线程程序运行正常不代表完全正确
我现在不是关心有没有拿到重复的票数,而是这2个线程,拿到了自己的对应票,却没有在拿到票之后打印出票号。票号按理论来讲是要100到1递减,但结果不是这样显示[/quote] 所以说要你加锁呀!!!
版主大哥 2014-10-15
  • 打赏
  • 举报
回复
问题来了,93号那张票,既然线程1抢到了,为何不是先输出 DWORD WINAPI Fun1Proc(LPVOID lpParameter) { while(TRUE) { if(tickets>0) cout<<"thread1 sell ticket :"<<tickets--<<endl; /./93号线程执行完tickets-- 准备执行cout<< 输出的时候发送线程切换,去执行线程2了,然后又回来就发生这种现象了 else break; } return 0; } DWORD WINAPI Fun2Proc(LPVOID lpParameter) { while(TRUE) { if(tickets>0) cout<<"thread2 sell ticket :"<<tickets--<<endl; else break; } return 0; } 多线程访问共享数据必须加锁
shenyi0106 2014-10-15
  • 打赏
  • 举报
回复
多线程的一个非常重要的概念就是——锁!!! 不理解锁,就别搞多线程了,这只会使你的思想更混乱
boylafong 2014-10-15
  • 打赏
  • 举报
回复
引用 2 楼 yang2009001 的回复:
[quote=引用 1 楼 lx624909677 的回复:] 给票数加锁,多线程程序运行正常不代表完全正确
我现在不是关心有没有拿到重复的票数,而是这2个线程,拿到了自己的对应票,却没有在拿到票之后打印出票号。票号按理论来讲是要100到1递减,但结果不是这样显示[/quote] 线程执行是不分先后的,就是说 Fun1Proc在执行tickets--的时候 Fun2Proc可能在执行cout, 这时候Fun2Proc全部执行完了, Fun1Proc才执行完cout
小哥该吃药了 2014-10-15
  • 打赏
  • 举报
回复
引用 1 楼 lx624909677 的回复:
给票数加锁,多线程程序运行正常不代表完全正确
我现在不是关心有没有拿到重复的票数,而是这2个线程,拿到了自己的对应票,却没有在拿到票之后打印出票号。票号按理论来讲是要100到1递减,但结果不是这样显示
lx624909677 2014-10-15
  • 打赏
  • 举报
回复
给票数加锁,多线程程序运行正常不代表完全正确

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧