MFC订座或者订票系统,如何实现多终端订票~? 支持多终端同时订票,并且能够在所有终端上实时显示出此时的座位情况。菜鸟跪求各位大神回答~>_< 解决方案 » 免费领取超大流量手机卡,每月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;}孙鑫VC++深入详解第十五章 火车票的一个简单示例 希望对你有用 多线程+临界区,把对票的加减操作都放在临界区进行:CRITICAL_SECTION cs; //最好是全局变量EnterCriticalSection(&cs); 加减操作LeaveCriticalSection(&cs); outlook编程 如何控制象素 DLL中调用其他DLL函数的问题,高手帮帮忙 想给自己估个价。 如何构造这样的IP包? 将别人写好的类加入到自己的项目中,出现问题。 如何格式化显示一个日期类型数据? 请教:各位大哥帮我看看下面几个问题,好吗?谢谢! 如何将BYTE ID[2],赋值给UNS32 id; 快来看呀!痛苦啊!大虾们帮忙呀!(龙虾?!) 求封装HTTP的C++库 VS2008,编译生成Release,报错error PRJ0019
#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;
}
孙鑫VC++深入详解第十五章 火车票的一个简单示例 希望对你有用
加减操作
LeaveCriticalSection(&cs);