| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 771 人关注过本帖
标题:利用多线程编一个售票系统,编译出的结果很奇怪???
只看楼主 加入收藏
kaiyanyixiao
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-4-17
收藏
 问题点数:0 回复次数:7 
利用多线程编一个售票系统,编译出的结果很奇怪???
这是我写的程序源代码:

#include<stdio.h>
#include<windows.h>
DWORD WINAPI Fun1Proc(
  LPVOID lpParameter   // thread data
);
DWORD WINAPI Fun2Proc(
  LPVOID lpParameter   // thread data
);

int index=0,ticket=100;

HANDLE hMutex;

void main()
{
 HANDLE pThread1;
 HANDLE pThread2;
 pThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
 pThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
 CloseHandle(pThread1);
 CloseHandle(pThread2);

 CreateMutex(NULL,FALSE,NULL);

 Sleep(4000);
}

DWORD WINAPI Fun1Proc(
  LPVOID lpParameter   // thread data
)
{
 while(TRUE)
 {
  WaitForSingleObject(hMutex,INFINITE);
  Sleep(1);
  if(ticket>0)
   printf("Thread1 sell ticket:%d\n",ticket);
  else
   break;
  ReleaseMutex(hMutex);
 }
 return 0;
}
DWORD WINAPI Fun2Proc(
  LPVOID lpParameter   // thread data
)
{
 while(TRUE)
 {
  WaitForSingleObject(hMutex,INFINITE);
  Sleep(1);
  if(ticket>0)
   printf("Thread1 sell ticket:%d\n",ticket--);
  else
   break;
  ReleaseMutex(hMutex);
 }
 return 0;
}


这是编译后的结果:


Thread1 sell ticket:100
Thread1 sell ticket:100
Thread1 sell ticket:99
Thread1 sell ticket:99
Thread1 sell ticket:99
Thread1 sell ticket:98
Thread1 sell ticket:98
Thread1 sell ticket:98
Thread1 sell ticket:97
Thread1 sell ticket:97
Thread1 sell ticket:96
Thread1 sell ticket:96
Thread1 sell ticket:96
Thread1 sell ticket:96
Thread1 seTh lreatdck1e ste:l ticket:95
94
Thread1 seTh lreatdck1e ste:l ticket:95
94
Thread1 sell ticket:94
Thread1 sell ticket:94
Thread1 sell ticket:94
Thread1 sell ticket:93
Thread1 sell ticket:93
Thread1 sell ticket:92
Thread1 sell ticket:92
Thread1 sell ticket:92
Thread1 sell ticket:91
Thread1 sell ticket:91
Thread1 sell ticket:91
Thread1 sell ticket:90
Thread1 sell ticket:90
Thread1 sell ticket:90
Thread1 sell ticket:89
Thread1 sell ticket:89
Thread1 sell ticket:89
Thread1 sell ticket:88
Thread1 sell ticket:88
Thread1 sell ticket:88
Thread1 sell ticket:87
Thread1 sell ticket:87
Thread1 sell ticket:87
Thread1 sell ticket:86
Thread1 sell ticket:86
Thread1 sell ticket:86
Thread1 sell ticket:85
Thread1 sell ticket:85
Thread1 sell ticket:85
Thread1 sell ticket:83
Thread1 sell ticket:83
Thread1 sell ticket:84
Thread1 sell ticket:83
Thread1 sell ticket:83
Thread1 sell ticket:83
Thread1 sell ticket:82
81
Thread1 sell ticket:81
Thread1 sell ticket:81
Thread1 sell ticket:81
Thread1 sell ticket:80
Thread1 sell ticket:80
Thread1 sell ticket:80
Thread1 sell ticket:79
Thread1 sell ticket:79
Thread1 sell ticket:79
Thread1 sell ticket:78
Thread1 sell ticket:78
Thread1 sell ticket:78
Thread1 sell ticket:77
Thread1 sell ticket:77
Thread1 sell ticket:77
Thread1 sell ticket:76
Thread1 sell ticket:76
Thread1 sell ticket:76
Thread1 sell ticket:75
Thread1 sell ticket:75
Thread1 sell ticket:75
Thread1 sell ticket:74
Thread1 sell ticket:74
Thread1 sell ticket:73
Thread1 sell ticket:73
hrThread1 sell ticket:73
hread1 sell ticket:72
Thread1 sell ticket:72
Thread1 sell ticket:72
Thread1 sell ticket:72
Thread1 sell ticket:71
Thread1 sell ticket:71
Thread1 sell ticket:71
Thread1 sell ticket:70
Thread1 sell ticket:70
Thread1 sell ticket:70
Thread1 sell ticket:69
Thread1 sell ticket:69
Thread1 sell ticket:69
Thread1 sell ticket:68
Thread1 sell ticket:68
Thread1 sell ticket:68
Thread1 sell ticket:67
Thread1 sell ticket:67
Thread1 sell ticket:66
Thread1 sell ticket:66
Thread1 sell ticket:66
Thread1 sell ticket:66
Thread1 sell ticket:65
Thread1 sell ticket:65
Thread1 sell ticket:65
Thread1 sell ticket:64
Thread1 sell ticket:64
Thread1 sell ticket:63
Thread1 sell ticket:63
Thread1 sell ticket:63
Thread1 sell ticket:63
Thread1 sell ticket:62
Thread1 sell ticket:62
Thread1 sell ticket:62
Thread1 sell ticket:61
Thread1 sell ticket:61
Thread1 sell ticket:61
Threredad1 selslll ttiiTceet:t:60

Threredad1 selslll ttiiTceet:t:60

Thread1 sell ticket:59
Thread1 sell ticket:59
Thread1 sell ticket:59
Thread1 sell ticket:58
Thread1 sell ticket:58
Thread1 sell ticket:58
Thread1 sell ticket:57
Thread1 sell ticket:57
Thread1 sell ticket:57
Thread1 sell ticket:56
Thread1 sell ticket:56
Thread1 sell ticket:55
Thread1 sell ticket:55
Thread1 sell ticket:55
Thread1 sell ticket:55
Thread1 sell ticket:54
Thread1 sell ticket:54
Thread1 sell ticket:54
Thread1 sell ticket:53
Thread1 sell ticket:53
Thread1 sell ticket:53
Thread1 sell ticket:52
Thread1 sell ticket:52
Thread1 sell ticket:52
Thread1 sell ticketThread1 sell ticket:51
Thread1 sell ticketThread1 sell ticket:51
:51
Thread1 sell ticket:50
Thread1 sell ticket:50
Thread1 sell ticket:49
Thread1 sell ticket:49
Thread1 sell ticket:49
Thread1 sell ticket:49
Thread1 sell ticket:48
Thread1 sell ticket:48
Thread1 sell ticket:47
Thread1 sell ticket:47
Thread1 sell ticket:47
Thread1 sell ticket:47
Thread1 sell ticket:46
Thread1 sell ticket:46
Thread1 sell ticket:46
Thread1 sell ticket:45
Thread1 sell ticket:45
Thread1 sell ticket:45
Thread1 sell ticket:44
Thread1 sell ticket:44
Thread1 sell ticket:44
Thread1 sell ticket:43
Thread1 sell ticket:43
Thread1 sell ticket:43
Thread1 sell ticket:42
Thread1 sell ticket:42
Thread1 sell ticket:42
Thread1 sell ticket:41
Thread1 sell ticket:41
Thread1 sell ticket:41
Thread1 sell ticket:40
Thread1 sell ticket:40
Thread1 sell ticket:40
Thread1 sell ticket:39
Thread1 sell ticket:39
Thread1 sell ticket:39
Thread1 sell ticket:38
Thread1 sell ticket:38
Thread1 sell ticket:38
Thread1 sell ticket:37
Thread1 sell ticket:37
Thread1 sell ticket:37
Thread1 sell ticket:36
Thread1 sell ticket:36
Thread1 sell ticket:36
Thread1 sell ticket:35
Thread1 sell ticket:35
Thread1 sell ticket:35
Thread1 sell ticket:34
Thread1 sell ticket:34
Thread1 sell ticket:34
Thread1 sell ticket:33
Thread1 sell ticket:33
Thread1 sell ticket:33
Thread1 sell ticket:32
Thread1 sell ticket:32
Thread1 sell ticket:32
Thread1 sell ticket:31
Thread1 sell ticket:31
Thread1 sell ticket:31
Thread1 sell ticket:30
Thread1 sell ticket:30
Thread1 sell ticket:30
Thread1 sell ticket:29
Thread1 sell ticket:29
Thread1 sell ticket:29
Thread1 sell ticket:28
Thread1 sell ticket:28
Thread1 sell ticket:28
Thread1 sell ticket:27
Thread1 sell ticket:27
Thread1 sell ticket:27
Thread1 sell ticket:26
Thread1 sell ticket:26
Thread1 sell ticket:26
Thread1 sell ticket:25
Thread1 sell ticket:25
Thread1 sell ticket:25
Thread1 sell ticket:24
Thread1 sell ticket:24
Thread1 sell ticket:24
Thread1 sell ticket:23
Thread1 sell ticket:23
Thread1 sell ticket:23
Thread1 sell ticket:22
Thread1 sell ticket:22
Thread1 sell ticket:22
Thread1 sell ticket:21
Thread1 sell ticket:21
Thread1 sell ticket:21
Thread1 sell ticket:20
Thread1 sell ticket:20
Thread1 sell ticket:20
Thread1 sell ticket:19
Thread1 sell ticket:19
Thread1 sell ticket:18
Thread1 sell ticket:18
Thread1 sell ticket:18
Thread1 sell ticket:18
Thread1 sell ticket:17
Thread1 sell ticket:17
Thread1 sell ticket:17
Thread1 sell ticket:16
Thread1 sell ticket:16
Thread1 sell ticket:16
Thread1 sell ticket:15
Thread1 sell ticket:15
Thread1 sell ticket:14
Thread1 sell ticket:14
Thread1 sell ticket:14
Thread1 sell ticket:13
Thread1 sell ticket:13
Thread1 sell ticket:13
Thread1 sell ticket:12
Thread1 sell ticket:12
Thread1 sell ticket:12
Thread1 sell ticket:11
Thread1 sell ticket:11
Thread1 sell ticket:11
Thread1 sell ticket:10
Thread1 sell ticket:10
Thread1 sell ticket:10
Thread1 sell ticket:9
Thread1 sell ticket:9
Thread1 sell ticket:9
Thread1 sell ticket:hread1 sell Thread1 sell ticket:hread1 sell ticket:8
Thread1 sell ticket:7
Thread1 sell ticket:7
Thread1 sell ticket:7
Thread1 sell ticket:6
Thread1 sell ticket:6
Thread1 sell ticket:6
Thread1 sell ticket:5
Thread1 sell ticket:5
Thread1 sell ticket:4
Thread1 sell ticket:4
Thread1 sell ticket:4
Thread1 sell ticket:4
Thread1 sell ticket:3
Thread1 sell ticket:3
Thread1 sell ticket:3
Thread1 sell ticket:2
Thread1 sell ticket:2
Thread1 sell ticket:2
Thread1 sell ticket:1
Thread1 sell ticket:1
Thread1 sell ticket:1
Press any key to continue

 

我的电脑配置:Intel(R)  core(TM)2 Duo   CPU T5470 @ 1.60GHz  751 MHz, 1.00G的内存。q

 请问这是怎么回事???为什么编译结果会这样???我写的代码哪里出现了问题???
搜索更多相关主题的帖子: 线程 系统 售票 编译 结果 
2008-05-20 11:38
crosoli
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2007-4-24
收藏
得分:0 
线程函数1里面的的ticket打应后没有自减,
if(ticket>0)
   printf("Thread1 sell ticket:%d\n",ticket);
应该是
2008-05-20 12:10
crosoli
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2007-4-24
收藏
得分:0 
不好意思,没写完
因该是
if(ticket>0)
   printf("Thread1 sell ticket:%d\n",ticket--);

然后2个函数的打应都是一样的?  那你怎么看的出多线程的效果呢?
2008-05-20 12:11
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
#include<stdio.h>
#include<windows.h>
DWORD WINAPI Fun1Proc(
  LPVOID lpParameter   // thread data
);
DWORD WINAPI Fun2Proc(
  LPVOID lpParameter   // thread data
);

int index=0,ticket=100;

HANDLE hMutex;

void main()
{
HANDLE pThread1;
HANDLE pThread2;
pThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
pThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(pThread1);
CloseHandle(pThread2);

CreateMutex(NULL,FALSE,NULL);

Sleep(4000);
}

DWORD WINAPI Fun1Proc(
  LPVOID lpParameter   // thread data
)
{
while(TRUE)
{
  WaitForSingleObject(hMutex,INFINITE);
  Sleep(1);
  if(ticket>0)
   printf("Thread1 sell ticket:%d\n",ticket--);
  else
   break;
  ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI Fun2Proc(
  LPVOID lpParameter   // thread data
)
{
while(TRUE)
{
  WaitForSingleObject(hMutex,INFINITE);
  Sleep(1);
  if(ticket>0)
   printf("Thread1 sell ticket:%d\n",ticket--);
  else
   break;
  ReleaseMutex(hMutex);
}
return 0;
}

学习需要安静。。海盗要重新来过。。
2008-05-20 12:22
kaiyanyixiao
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-4-17
收藏
得分:0 
将楼上代码粘贴复制后,将到说第七行的“printf("Thread1 sell ticket:%d\n",ticket--);”Thread1改为Thread2进行编译,输出结果为:
Thread1 sell ticket:100
Thread2 sell ticket:99
Thread1 sell ticket:98
Thread2 sell ticket:97
Thread1 sell ticket:96
Thread1 sell ticket:96
Thread2 sell ticket:95
Thread1 sell ticket:94
Thread2 sell ticket:93
Thread1 sell ticket:92
Thread1 sell ticket:92
Thread2 sell ticket:91
Thread1 sell ticket:90
Thread2 sell ticket:89
Thread1 sell ticket:88
Thread1 sell ticket:88
Thread2 sell ticket:87
Thread1 sell ticket:86
Thread1 sell ticket:86
Thread2 sell ticket:85
Thread1 sell ticket:84
Thread1 sell ticket:84
Thread2 sell ticket:83
Thread1 sell ticket:82
Thread1 sell ticket:82
Thread2 sell ticket:81
Thread1 sell ticket:80
Thread1 sell ticket:80
Thread2 sell ticket:79
Thread1 sell ticket:78
Thread1 sell ticket:78
Thread2 sell ticket:77
Thread1 sell ticket:76
Thread1 sell ticket:76
Thread2 sell ticket:75
Thread1 sell ticket:74
Thread1 sell ticket:74
Thread2 sell ticket:73
Thread1 sell tihrcakd2 ts:ell ticket:2
Thread1 sell tihrcakd2 ts:ell ticket:2
71
Thread1 sell ticket:70
Thread2 sell ticket:69
Thread1 sell ticket:68
Thread1 sell ticket:68
Thread2 sell ticket:67
Thread1 sell ticket:66
Thread1 sell ticket:66
Thread2 sell ticket:65
Thread2 sell ticket:64
Thread2 sell ticket:64
Thread1 sell ticket:63
Thread1 sell ticket:62
Thread1 sell ticket:62
Thread2 sell ticket:61
Thread1 sell ticket:60
Thread1 sell ticket:60
Thread2 sell ticket:59
Thread1 sell ticket:58
Thread1 sell ticket:58
Thread2 sell ticket:57
Thread1 sell ticket:56
Thread1 sell ticket:56
Thread2 sell ticket:55
Thread1 sell ticket:54
Thread1 sell ticket:54
Thread2 sell ticket:53
Thread1 sell ticket:52
Thread1 sell ticket:52
Thread2 sell ticket:51
Thread1 sell ticket:50
Thread1 sell ticket:50
Thread2 sell ticket:49
Thread1 sell ticket:48
Thread1 sell ticket:48
Thread2 sell ticket:47
Thread1 sell ticket:46
Thread1 sell ticket:46
Thread2 sell ticket:45
Thread2 sell Ticrekaed:1 sell tick44t
:Thread2 sell Ticrekaed:1 sell tick44t
:43
Thread1 sell ticket:42
Thread1 sell ticket:42
Thread2 sell ticket:41
Thread1 sell ticket:40
Thread1 sell ticket:40
Thread2 sell ticket:39
Thread1 sell ticket:38
Thread1 sell ticket:38
Thread2 sell ticket:37
Thread1 sell ticket:36
Thread1 sell ticket:36
Thread2 sell ticket:35
Thread1 sell ticket:34
Thread1 sell ticket:34
Thread2 sell ticket:33
Thread1 sell ticket:32
Thread1 sell ticket:32
Thread2 sell ticket:31
Thread2 sell ticket:30
Thread2 sell ticket:30
Thread1 sell ticket:29
Thread1 sell ticket:28
Thread1 sell ticket:28
Thread2 sell ticket:27
Thread1 sell ticket:26
Thread1 sell ticket:26
Thread2 sell ticket:25
Thread1 sell ticket:24
Thread1 sell ticket:24
Thread2 sell ticket:23
Thherdea1d selsel l ticckteat:2222

Thherdea1d selsel l ticckteat:2222

Thread1 sell ticket:20
Thread1 sell ticket:20
Thread2 sell ticket:19
Thread1 sell ticket:18
Thread1 sell ticket:17
Thread1 sell ticket:17
Thread2 sell ticket:16
ThrTad1r asedtl2 stellc kteact:set:1514

ThrTad1r asedtl2 stellc kteact:set:1514

Thread1 sell ticket:13
Thread1 sell ticket:13
Thread2 sell ticket:12
TThrreeadedl1 seell:  tckei:cket:1011

TThrreeadedl1 seell:  tckei:cket:1011

Thread1 sell ticket:9
Thread1 sell ticket:9
Thread2 sell ticket:8
Thread1 sell ticket:7
Thread1 sell ticket:7
Thread2 sell ticket:6
Thread1 sell ticket:5
Thread1 sell ticket:5
Thread2 sell ticket:4
Thread1 sell ticket:3
Thread1 sell ticket:3
Thread2 sell ticket:2
Thread1 sell ticket:1
Press any key to continue
从输出结果看有两个问题我不明白:1、为什么会出现相同的票号被销售,例如3出现了两次?2、为什么输出结果很不规则,如一下这几行:
Thread1 sell ticket:74
Thread2 sell ticket:73
Thread1 sell tihrcakd2 ts:ell ticket:2
Thread1 sell tihrcakd2 ts:ell ticket:2
71
Thread1 sell ticket:70
Thread2 sell ticket:69
而且每次编译后这些不规则的代码都会出现在不同的地方???

爱拼才会赢!
2008-05-20 15:18
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
#include<stdio.h>
#include<windows.h>
DWORD WINAPI Fun1Proc(
  LPVOID lpParameter   // thread data
);
DWORD WINAPI Fun2Proc(
  LPVOID lpParameter   // thread data
);

int index=0,ticket=100;

HANDLE hMutex;

void main()
{
HANDLE pThread1;
HANDLE pThread2;
hMutex=CreateMutex(NULL,FALSE,NULL);
pThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
pThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(pThread1);
CloseHandle(pThread2);

CreateMutex(NULL,FALSE,NULL);

Sleep(4000);
}

DWORD WINAPI Fun1Proc(
  LPVOID lpParameter   // thread data
)
{
while(TRUE)
{
  WaitForSingleObject(hMutex,INFINITE);
  Sleep(1);
  if(ticket>0)
   printf("Thread1 sell ticket:%d\n",ticket--);
  else
   break;
  ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI Fun2Proc(
  LPVOID lpParameter   // thread data
)
{
while(TRUE)
{
  WaitForSingleObject(hMutex,INFINITE);
  Sleep(1);
  if(ticket>0)
   printf("Thread1 sell ticket:%d\n",ticket--);
  else
   break;
  ReleaseMutex(hMutex);
}
return 0;
}

学习需要安静。。海盗要重新来过。。
2008-05-20 15:59
kaiyanyixiao
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-4-17
收藏
得分:0 
谢谢楼上的朋友们,我的问题解决了。

爱拼才会赢!
2008-05-20 16:26
lijun86725
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-4-11
收藏
得分:0 
我的程序也出现同样问题,怎样解决的?
2012-04-11 14:18
快速回复:利用多线程编一个售票系统,编译出的结果很奇怪???
数据加载中...
 
   



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

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