支持多终端同时订票,并且能够在所有终端上实时显示出此时的座位情况。
菜鸟跪求各位大神回答~>_<

解决方案 »

  1.   

    #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++深入详解第十五章 火车票的一个简单示例 希望对你有用
      

  2.   

    多线程+临界区,把对票的加减操作都放在临界区进行:CRITICAL_SECTION cs; //最好是全局变量EnterCriticalSection(&cs);
     加减操作
    LeaveCriticalSection(&cs);