是尘封已久的记忆再次融化 是堆积心底的烈火再次燃烧 是直觉让我来到这久违的大地 是信念让我开始了新的征途 在硝烟中我得到了成长 在伤痛中我学会了坚强 在沉默中我明白了等待 在孤独中我体味了感伤 并不是我不懂得眼泪 并不是我只知道使命 在内心深处我同你一样火热 在我的眼中也有着多情的泪光 也许我的生命如落叶般短暂 也许我只是岁月长河中的一个过客 但我对自己所做的一切无怨无悔 因为我品尝到了那最后一刻的泪光
随笔-6  评论-24  文章-8  trackbacks-0

 

#define N 6

HANDLE hSuspend[N], hResume[N];
HANDLE hSuspend_one;
HANDLE hResume_one;

struct info
{
    CRITICAL_SECTION  ProtectSection;
    
int id;
    
int frames;
}
;


unsigned 
long __stdcall testFun(void *pContext)
{
    info
* pInfo = (info*)pContext;
    
while(1)
    
{    
        EnterCriticalSection(
&pInfo->ProtectSection);
        pInfo
->frames++;    
        
//printf("run sub Thread  video %d frames %d\n", pInfo->id, pInfo->frames);
    
//    DWORD rtn = WaitForSingleObject(hSuspend[pInfo->id], INFINITE);
        DWORD rtn = WaitForSingleObject(hSuspend_one, INFINITE);
        
if (WAIT_OBJECT_0 == rtn)
        
{// 自己暂停自己
            
        
//    printf("stop sub Thread video %d frames %d\n", pInfo->id, pInfo->frames);
            WaitForSingleObject(hResume[pInfo->id], INFINITE);
            
//WaitForSingleObject(hResume_one, INFINITE);
        }

        LeaveCriticalSection(
&pInfo->ProtectSection);
    }

    
return 0;
}



int main()
{
    
    DWORD thid;
    HANDLE hand[N];
    
    
int mainframes = 0;
    info myInfo[N];

    hSuspend_one 
= CreateEvent(NULL, TRUE, FALSE, NULL);
    
for(int i = 0; i < N; i++)
    
{
        hSuspend[i] 
= CreateEvent(NULL, TRUE, FALSE, NULL);
        hResume[i] 
= CreateEvent(NULL, FALSE, FALSE, NULL);
        myInfo[i].id 
= i;
        myInfo[i].frames 
= 0;
        InitializeCriticalSection(
&myInfo[i].ProtectSection);
    }

    
for(int i = 0; i < N; i++)
    
{
        hand[i] 
= CreateThread( NULL, NULL, testFun, &myInfo[i], CREATE_SUSPENDED, &thid);
    }


    
for(int i = 0; i < N; i++)
    
{
        ResumeThread(hand[i]);
    }

    
while(1)
    
{
        printf(
"main thread frames %d\n"++mainframes);
        
/*for(int i = 0; i < N; i++)
        {        
            SetEvent(hSuspend[i]);
        }
*/

        SetEvent(hSuspend_one);
        
        Sleep(
5);
        
for(int i = 0; i < N; i++)
        
{    
            printf(
"main thread video %d frames %d\n",myInfo[i].id,myInfo[i].frames);    
        }

        printf(
"\n");
        
        
for(int i = 0; i < N; i++)
        
{            
            SetEvent(hResume[i]);
        }

    }

    
    
for(int i = 0; i < N; i++)
    
{
        WaitForSingleObject(hand[i], INFINITE);    
        DeleteCriticalSection( 
&myInfo[i].ProtectSection);
        CloseHandle(hand[i]);
        CloseHandle(hResume[i]);
        CloseHandle(hSuspend[i]);
    }

    CloseHandle(hSuspend_one);
    
return 0;
}

 

posted on 2012-04-06 13:32 noBugnoGain 阅读(936) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理