谁能给个windows下多线程+互斥锁的简单例子? 如题!我主要是想参考一下步骤。自己写的老是运行有问题!~谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #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;HANDLE hMutex;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);// hMutex=CreateMutex(NULL,TRUE,NULL); hMutex=CreateMutex(NULL,TRUE,"tickets"); if(hMutex) { if(ERROR_ALREADY_EXISTS==GetLastError()) { cout<<"only one instance can run!"<<endl; return; } } WaitForSingleObject(hMutex,INFINITE); ReleaseMutex(hMutex); ReleaseMutex(hMutex); Sleep(4000);}DWORD WINAPI Fun1Proc( LPVOID lpParameter // thread data){/* while(TRUE) { WaitForSingleObject(hMutex,INFINITE); if(tickets>0) { Sleep(1); cout<<"thread1 sell ticket : "<<tickets--<<endl; } else break; ReleaseMutex(hMutex); }*/ WaitForSingleObject(hMutex,INFINITE); cout<<"thread1 is running"<<endl; return 0;}DWORD WINAPI Fun2Proc( LPVOID lpParameter // thread data){/* while(TRUE) { WaitForSingleObject(hMutex,INFINITE); if(tickets>0) { Sleep(1); cout<<"thread2 sell ticket : "<<tickets--<<endl; } else break; ReleaseMutex(hMutex); }*/ WaitForSingleObject(hMutex,INFINITE); cout<<"thread2 is running"<<endl; return 0;} vc2008编译获取光标的问题 关于FindFirstFile FindNextFile 过程中的文件权限问题 请问怎么实现从正方形逐渐变成圆的?? 怎么让一个浮点数只显示有效位数,后面没有用的零不显示 广而告之:公司专场招聘信息 请问,如何实现象acdsee中的图像连放功能 “VC,求访名师” 最终贴 之散分贴1 帮我看看这段代码! vc 6.0 执行和调试都没有反应?? 关于ug二次开发中出现的无法解析的外部符号,求助大神 数据文件怎么打包 MFC中,TextOutW()函数怎么用?它与TextOut()的区别?
#include <iostream.h>DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
);
int index=0;
int tickets=100;
HANDLE hMutex;
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);// hMutex=CreateMutex(NULL,TRUE,NULL);
hMutex=CreateMutex(NULL,TRUE,"tickets");
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only one instance can run!"<<endl;
return;
}
}
WaitForSingleObject(hMutex,INFINITE);
ReleaseMutex(hMutex);
ReleaseMutex(hMutex); Sleep(4000);
}DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
/* while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread1 is running"<<endl; return 0;
}DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
)
{
/* while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread2 is running"<<endl;
return 0;
}