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

小哥该吃药了 小哥该吃药了 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
今天在学习多线程编程时,在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号票等,每次运行都有几个这样的情况。
...全文

相关推荐

解决js的md5中文和java不一致的情况

解决js的md5中文和java不一致的情况

sxm198256

关于ftp下显示的时间和系统时间不一致的解决方法

ftp上来显示的时间和系统时间不一致,是因为默认情况下,vsftpd 是用GMT做为他的时间的,所以和系统的时间可能会不一致

weixin_38697808

C# 不使用Task实现的多线程顺序执行

多线程有很好的并发性即无序性,在某些特殊情况下需要用到多线程然而又要使其具备顺序性,这种时候就有了一个特殊的场景那就是多线程顺序执行,在现在VS2015中Task自带了顺序执行的方法,但在此之前的旧项目中如果需要使用多线程顺序执行该怎么办呢?Task又是怎么实现的呢?这里提供了一种类似Task顺序执行多线程的实现(可能跟Task的原理有很大区别),希望对大家能有帮助,对此类问题的实现有更好的思路和技巧的同学欢迎留言,大家一起探讨。

喜悦自习室HiriswClassRoom

PyQt5多线程的执行和停止

实现三个线程同时执行,且可以单独关闭和运行,通过进度条实现可视化演示。

酷酷咕咕

在父窗口不透明的情况下,实现子窗口的半透明效果

根据MSDN所述,子窗口的半透属性是跟随父窗口的。也就是说在子窗口中用SetLayeredWindowAttributes方法完成透明是不可行的。本人偷巧,在父窗口不透明情况下,简单实现子窗口的半透明效果。

feather66
评论 7
收藏 已收藏
分享
返回顶部