读书人

几乎是孙鑫的源程序不知错在哪儿

发布时间: 2012-08-25 10:06:20 作者: rapoo

几乎是孙鑫的源程序,不知错在哪里

#include <iostream>
#include <windows.h>
using namespace std;

DWORD WINAPI fun1proc(LPVOID);
DWORD WINAPI fun2proc(LPVOID);

int index=0;
int tickets=100;
HANDLE hMutex;
int main()
{
HANDLE hThread1,hThread2;
HANDLE hMutex;
//hThread1=CreateThread(NULL,0,funproc,NULL,CREATE_SUSPENDED,NULL);
//ResumeThread(hThread1);
hMutex=CreateMutex(NULL,false,NULL);
hThread1=CreateThread(NULL,0,fun1proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,fun2proc,NULL,0,NULL);

CloseHandle(hThread1);
CloseHandle(hThread2);
//cout<<"main thread is running\n";
Sleep(40000);
// while(TRUE)
// Sleep(100);
return 0;
}

DWORD WINAPI fun1proc(LPVOID )
{
while(TRUE)
{
//WaitForSingleObject(hMutex,1000);
WaitForSingleObject(hMutex,INFINITE);
if (tickets>0)
{
cout<<"thread1 is selling tickets"<<tickets--<<endl;
//ReleaseMutex(hMutex);
}
else
break;
ReleaseMutex(hMutex);
}
//while(index++<1000);
//cout<<"thread is running\n";
return 0;
}

DWORD WINAPI fun2proc(LPVOID )
{
while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if (tickets>0)
{
cout<<"thread2 is selling tickets"<<tickets--<<endl;

}
else
break;
ReleaseMutex(hMutex);
}
//while(index++<1000);
//cout<<"thread is running\n";
return 0;
}




运行结果:
thread1 is setlhlrienadg2 its iseclkets100
thread1 liisn g stieclkletisn9g tickets98
thread1 is9 selling tickets9
7th
rtehadr2e aids 1s eils selling tickets95
thrlead1 is selling tiickets94
thread1 isn selling tigc kteictkset9s396
thread1 is sell
ing tickets92
thretad1 is selling tichkets91
thread1 is rselling tickets89
ethread1 is selling tickets88
thread1 iasd s2el ling ticketis sse8l7l
ithread1 is selling tickets86
thread1 is selling tickets85
thread1 isn selling tickets84g
thread1 is sellin g tickets83
threadt1 is selling tickeits82
thread1 is seclling tickets81
thkread1 is selling teickets80
thread1 its selling tickets7s9
thread1 is sell90
ithreng tickets78
thread1 is selling tickets76
thread1 is selling tickets7a5d
2 itsh rseealldi1 is selling tickets74
ntgh triecakedts717
ist hsellingr etaidcke2t s73
itsh rseelalidng1 is selltickeitsn7g ti2
cktherteasd71
thread1 is selling tickets69
2t ihsr esaeldli1ng itisc kestesl7l0i
ntgh rteiacdk2e tiss6 8s
etlhlrienagd 1t iicsk estesl6l7i
ntgh rteiacdk2e tiss6 6s
etlhlrienagd 1t iicsk estesl6l5i
ngt htriecakde2t si6s4
stehlrleiandg1 tiisc kseetlsl6i3n
g tthircekaedt2s 6i2s
thresealdl1in is selling tigc kets6tic0k
ettshread61 is se1l
lithrnega dt2i cikse tsse5l9l
itnhgr etaidc1k eitss 5s8e
lltihnrge atdi2c kiest ss5e7l
ltihnrge atdi1c kiest ss5e6l
litnhgr etaidc2k eitss 5s5e
ltlhirnega dt1i cikse tsse5l4l
itnhgr etaidc2k eitss 5selling3 t
itchkertesa52d1 is selling tickets51

tthhrreeaadd21 iiss sseelllliinngg ttiicckkeettss5409

tthhrreeaadd21 iiss sseelllliinngg ttiicckkeettss4487

tthhrreeaadd21 iiss sseelllliinngg ttiicckkeettss4465

tthhrreeaadd21 iiss sseelllliinngg ttiicckkeettss4443

tthhrreeaadd21 iiss sseelllilnign tgi ctkiectske4ts41
2t
hrteharde1a di2s isse lsleilnlgi ntgi ctkiectkse4t0s
3t9h
rteharde1a di2s isse lsleilnlgi ntgi ctkiectkse3t8s
3t7h
retahdr1e aids2 siesl lsienlgli ntgi ctkiectkse3t6s
3t5h
retahdr1e aids2 siesl lsienlgl itnigc kteitcsk3e4t


st3h3r
eatdh1r eiasd 2s eilsl isnegl ltiincgk ettisc3k2e
tst31
htrheraeadd12 is selling itsi cseklleitngs t3i0c
thread1 is skeetlsli2n9g
ttihcrkeeatds22 8i
st hsreelaldi1n gi st iscekleltisn2g7
titchkreetasd226
ist hsreelaldi1n gi st iscekleltisn2g5
ttihcrkeeatds22 4i
st hsreelaldi1n gi st iscekleltisn2g3
titchkreetasd222
itsh rseealdl1i nigs tsieclkleitnsg2 1t
itchkreetasd2 2is selling ticke0t
st1h9r
eatdh1r eiasd 2s eilsl isnegl ltiincgk ettisc1k8e
ttsh1r7e
adt1h riesa ds2e lilsi nsge ltliicnkge ttsi1c6k
etthsr1e5a
dt1h riesa ds2e lilsi nsge ltliicnkge ttsi1c4k
etthsr1e3a
d1t hirse asde2l liisn gs etlilciknegt st1i2c
ktehtrse1a1d
1 thiresa ds2 eislling tickets10
setllhirnge atidc1k eitss 9s
eltlhirnega dt2i cikse tsse8l
ltihnrge atdi1c kiest ss7e
ltlhirnega dt2i cikse tsse6l
ltihnrge atdi1c kiest ss5e
ltlhirnega dt2i cikse tsse4l
lingt htriecakde1t si3s
tsherlelaidn2g itsi cskeeltlsi2n
g tickets1
Press any key to continue

[解决办法]
HANDLE hMutex;int main()
{
HANDLE hThread1,hThread2;
HANDLE hMutex;
//hThread1=CreateThread(NULL,0,funproc,NULL,CREATE_SUSPENDED,NULL);
//ResumeThread(hThread1);
hMutex=CreateMutex(NULL,false,NULL);
hThread1=CreateThread(NULL,0,fun1proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,fun2proc,NULL,0,NULL);

CloseHandle(hThread1);
CloseHandle(hThread2);
//cout<<"main thread is running\n";
Sleep(40000);
// while(TRUE)
// Sleep(100);
return 0;
}

局部的hMutex屏蔽了全局的hMutex

读书人网 >C++

热点推荐