主线程和子线程互斥问题

cschengvdn 2011-07-13 05:01:44
大家看看我这个程序,我想让主线程和子线程交替执行,但是最终的结果是子线程没得到执行,而两个都是子线程时就可以为什么换成是主线程就不行了:
#include <windows.h>
#include <iostream>
using namespace std;

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


int index=0;
int tickets=100;
HANDLE hMutex;
void main()
{
HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);

hMutex=CreateMutex(NULL,TRUE,NULL);
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only one instance can run!"<<endl;
return;
}
}

while(TRUE)
{
int a=WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}


cout<<"thread2 is running"<<endl;
Sleep(4000);
}

DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
while(TRUE)
{
int a=WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}


return 0;
}


最后的结果::
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
thread2 is running
...全文
171 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cschengvdn 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 visualeleven 的回复:]

引用 4 楼 cschengvdn 的回复:

引用 1 楼 visualeleven 的回复:

C/C++ code
#include <windows.h>
#include <iostream>

using namespace std;

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


……
[/Quote]
呵呵,不好意思,有个地方改的没看到,多谢了
Eleven 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cschengvdn 的回复:]

引用 1 楼 visualeleven 的回复:

C/C++ code
#include <windows.h>
#include <iostream>
using namespace std;

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


int index=0;
int t……
还是……
[/Quote]
你有运行吗?
cschengvdn 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhupf 的回复:]

hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
可能立马执行子线程,然后子线程 int a=WaitForSingleObject(hMutex,INFINITE);
会调用失败。接着咋了?你调试下看。

所以我支持2楼那种。或者传递hMutex作为线程参数给子线程。
[/Quote]
确实是执行了一下子线程 int a=WaitForSingleObject(hMutex,INFINITE);,但是立马退出子线程,然后再也没进入子线程了
cschengvdn 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 visualeleven 的回复:]

C/C++ code
#include <windows.h>
#include <iostream>
using namespace std;

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


int index=0;
int t……
[/Quote]还是跟原来一样啊
不说害怕 2011-07-13
  • 打赏
  • 举报
回复
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
可能立马执行子线程,然后子线程 int a=WaitForSingleObject(hMutex,INFINITE);
会调用失败。接着咋了?你调试下看。

所以我支持2楼那种。或者传递hMutex作为线程参数给子线程。
呔妖怪来嘛 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 visualeleven 的回复:]
C/C++ code
#include <windows.h>
#include <iostream>
using namespace std;

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


int index=……
[/Quote]


if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
}
else
{
ReleaseMutex(hMutex);
break;
}


大仙。。。继续膜拜
Eleven 2011-07-13
  • 打赏
  • 举报
回复
#include <windows.h>
#include <iostream>
using namespace std;

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


int index=0;
int tickets=100;
HANDLE hMutex;
void main()
{
hMutex=CreateMutex(NULL,FALSE,NULL);
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only one instance can run!"<<endl;
return;
}
}

HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);

while(TRUE)
{
int a=WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
}
else
{
ReleaseMutex(hMutex);
break;
}
ReleaseMutex(hMutex);
}


cout<<"thread2 is running"<<endl;
Sleep(4000);
}

DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
while(TRUE)
{
int a=WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
}
else
{
ReleaseMutex(hMutex);
break;
}
ReleaseMutex(hMutex);
}


return 0;
}

15,472

社区成员

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

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