| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 433 人关注过本帖
标题:多线程的为啥不输出“only one”谢谢
取消只看楼主 加入收藏
renhongjin
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-3-8
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
多线程的为啥不输出“only one”谢谢
#include<windows.h>
#include<iostream.h>


DWORD WINAPI Fun1Proc(LPVOID lpParameeter);
DWORD WINAPI Fun2Proc(LPVOID lpParameeter);


int tickets=100;
HANDLE g_hEvent;


void main()
{
    HANDLE hThread1;
    HANDLE hThread2;

    g_hEvent=CreateEvent(NULL,FALSE,FALSE,"tickets");
    if(g_hEvent)
    {
        if(ERROR_ALREADY_EXISTS==GetLastError())
        {
            cout<<"only one "<<endl;
            return ;
        }
    }

    SetEvent(g_hEvent);
    hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
    hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);

    CloseHandle(hThread1);
    CloseHandle(hThread2);

    Sleep(400);

    CloseHandle(g_hEvent);

}


DWORD WINAPI Fun1Proc(LPVOID lpParameeter)
{
    while(TRUE)
    {
        
        WaitForSingleObject(g_hEvent,INFINITE);//都可以请求
//        ResetEvent(g_hEvent);//都可以请求
        if(tickets>0)
        {
            //Sleep(1);
            cout<<"Thread1 sell ticket:"<<tickets--<<endl;
        }
        else
            break;
        SetEvent(g_hEvent);
    }

    return 0;
}

DWORD WINAPI Fun2Proc(LPVOID lpParameeter)
{
    while(TRUE)
    {
        WaitForSingleObject(g_hEvent,INFINITE);
//        ResetEvent(g_hEvent);
        if(tickets>0)
        {
            //Sleep(1);
            cout<<"Thread2 sell ticket:"<<tickets--<<endl;
        }
        else
            break;
        SetEvent(g_hEvent);
    }

    return 0;
}
搜索更多相关主题的帖子: return include only tickets 
2013-03-25 22:15
快速回复:多线程的为啥不输出“only one”谢谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057563 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved