| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 672 人关注过本帖
标题:孙鑫VC++第15章多线程售票,最前边的票(本例为100,有时是99),卖出两张, ...
只看楼主 加入收藏
lijun86725
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-4-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
孙鑫VC++第15章多线程售票,最前边的票(本例为100,有时是99),卖出两张,怎么解决?
如题。
Win32 Console Application类型的工程,添加的C++源文件:
#include <windows.h>
#include <iostream.h>

DWORD WINAPI Fun1Proc(
  LPVOID lpParameter   // thread data
);

DWORD WINAPI Fun2Proc(
  LPVOID lpParameter   // thread data
);
int index=0;
int tickets=100;
void main()
{
    HANDLE hThread1;
    HANDLE hThread2;
    hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
    hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
    CloseHandle(hThread1);
    CloseHandle(hThread2);
    Sleep(4000);
}

DWORD WINAPI Fun2Proc(
  LPVOID lpParameter   // thread data
)
{
    while(TRUE)
    {   
        
        if(tickets>0)
        {
            cout<<"thread2 sell ticket : "<<tickets--<<endl;

        }
        else
            break;
        
    }
return 0;
}



DWORD WINAPI Fun1Proc(
  LPVOID lpParameter   // thread data
)
{
    while(TRUE)
    {   
        
        if(tickets>0)
        {
            cout<<"thread1 sell ticket : "<<tickets--<<endl;

        }
        else
            break;
        
    }
return 0;
}


搜索更多相关主题的帖子: 100 void include thread 多线程 
2012-04-11 14:33
lijun86725
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-4-11
收藏
得分:0 
运行结果:


thread1 sell ticket : 100
thread1 sell ticket : 99
thread1 sell ticket : 99
thread2 sell ticket : 98
thread1 sell ticket : 97
thread2 sell ticket : 96
thread1 sell ticket : 95
thread2 sell ticket : 94
thread1 sell ticket : 93
thread2 sell ticket : 92
thread1 sell ticket : 91
thread2 sell ticket : 90
thread1 sell ticket : 89
thread2 sell ticket : 88
thread1 sell ticket : 87
thread2 sell ticket : 86
thread1 sell ticket : 85
thread2 sell ticket : 84
thread1 sell ticket : 83
thread2 sell ticket : 82
thread1 sell ticket : 81
thread2 sell ticket : 80
thread1 sell ticket : 79
thread2 sell ticket : 78
thread1 sell ticket : 77
thread2 sell ticket : 76
thread1 sell ticket : 75
thread2 sell ticket : 74
thread1 sell ticket : 73
thread2 sell ticket : 72
thread1 sell ticket : 71
thread2 sell ticket : 70
thread1 sell ticket : 69
thread2 sell ticket : 68
thread1 sell ticket : 67
thread2 sell ticket : 66
thread1 sell ticket : 65
thread2 sell ticket : 64
thread1 sell ticket : 63
thread2 sell ticket : 62
thread1 sell ticket : 61
thread2 sell ticket : 60
thread1 sell ticket : 59
thread2 sell ticket : 58
thread1 sell ticket : 57
thread2 sell ticket : 56
thread1 sell ticket : 55
thread2 sell ticket : 54
thread1 sell ticket : 53
thread2 sell ticket : 52
thread1 sell ticket : 51
thread2 sell ticket : 50
thread1 sell ticket : 49
thread2 sell ticket : 48
thread1 sell ticket : 47
thread2 sell ticket : 46
thread1 sell ticket : 45
thread2 sell ticket : 44
thread1 sell ticket : 43
thread2 sell ticket : 42
thread1 sell ticket : 41
thread2 sell ticket : 40
thread1 sell ticket : 39
thread2 sell ticket : 38
thread1 sell ticket : 37
thread2 sell ticket : 36
thread1 sell ticket : 35
thread2 sell ticket : 34
thread1 sell ticket : 33
thread2 sell ticket : 32
thread1 sell ticket : 31
thread2 sell ticket : 30
thread1 sell ticket : 29
thread2 sell ticket : 28
thread1 sell ticket : 27
thread2 sell ticket : 26
thread1 sell ticket : 25
thread2 sell ticket : 24
thread1 sell ticket : 23
thread2 sell ticket : 22
thread1 sell ticket : 21
thread2 sell ticket : 20
thread1 sell ticket : 19
thread2 sell ticket : 18
thread1 sell ticket : 17
thread2 sell ticket : 16
thread1 sell ticket : 15
thread2 sell ticket : 14
thread1 sell ticket : 13
thread2 sell ticket : 12
thread1 sell ticket : 11
thread2 sell ticket : 10
thread1 sell ticket : 9
thread2 sell ticket : 8
thread1 sell ticket : 7
thread2 sell ticket : 6
thread1 sell ticket : 5
thread2 sell ticket : 4
thread1 sell ticket : 3
thread2 sell ticket : 2
thread1 sell ticket : 1
Press any key to continue
2012-04-11 14:39
lijun86725
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-4-11
收藏
得分:0 
孙老师的执行结果:thread sell ticket:100-----67
thread sell ticket:66----

为什么我的程序两个线程交替的这么频繁?分配的时间片短?
2012-04-11 15:31
lijun86725
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-4-11
收藏
得分:0 
请高手将上面代码复制运行一下,贴出结果看一下!在此深表感谢!!
2012-04-11 17:28
yuccn
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:10 
你没有发现你的代码出现问题了吗?
thread1 sell ticket : 99
 thread1 sell ticket : 99
 thread2 sell ticket : 98

 买了两张99的了,没有锁的后果~~

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2012-04-13 22:36
smilekurt
该用户已被删除
收藏
得分:10 
提示: 作者被禁止或删除 内容自动屏蔽
2012-04-16 10:34
快速回复:孙鑫VC++第15章多线程售票,最前边的票(本例为100,有时是99),卖出 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027225 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved