您的位置:首页 > 其它

多线程的时候遇到的EVENT的问题

2016-01-07 21:21 302 查看
当线程同步的线程数量超过64个的时候,出现WaitForMultipleObjectsEx函数等待的时候无效。该问题怀疑是内核态有这个数量限制

#include <iostream>
using namespace std;

#include <process.h>
#include <Windows.h>
#include <sstream>
#include <direct.h>
#include <time.h>
#include <string.h>

RTL_CRITICAL_SECTION g_fMutex;
HANDLE tongbu;

int record()
{
    FILE* pFile = fopen("1.txt", "wb+");
    fwrite("1234", 1, strlen("1234"), pFile);
    fclose(pFile);
    return 0;
}

unsigned __stdcall record_vedio(void * pIn)
{
    char szBuff[MAX_PATH] = {0};
    memcpy(szBuff, pIn, strlen((const char* )pIn));
    SetEvent(tongbu);
    Sleep(0);
    stringstream CurrentPath;
    char currentTime[64] = {0};
    char startTime[64] = {0};
    char endTime[64] = {0};
    
    time_t currentTimeTmp = time(NULL);
    tm* currentTime1 = localtime(¤tTimeTmp);
    strftime(currentTime, 64, "%Y%m%d", currentTime1);
    
    ::EnterCriticalSection(&g_fMutex);   /* 临界区 */
    //cout << "hello" << endl;
    cout << szBuff << endl;
    
    #if 0
    mkdir(szBuff);
    
    CurrentPath << szBuff << "\\" << currentTime;
    mkdir(CurrentPath.str().c_str());
    
    /* %H%M%S000000 */
    memset(currentTime, 0, sizeof(currentTime));
    CurrentPath.str("");
    strftime(startTime, 64, "%H%M%S", currentTime1);
    CurrentPath << "\\" << startTime << "000000" << ".h3c";
    FILE *pFile = fopen(CurrentPath.str().c_str(), "wb+");
    Sleep(5000);
    fclose(pFile);
    rename()
    #endif
    
    ::LeaveCriticalSection(&g_fMutex);
    return 0;
}

#define MAX_THREAD 65

int main(int argc, char** argv)
{
    const char* pExePath = argv[0];
    
    const char* pDivPath = strrchr(pExePath, '\\');
    
    char szPath[MAX_PATH] = {0};
    
    memcpy(szPath, pExePath, pDivPath-pExePath);
    
    cout << szPath << endl;
    
    tongbu = CreateEvent(NULL, FALSE, FALSE, NULL);
    stringstream cameraName[MAX_THREAD];
    for(int i = 0; i < MAX_THREAD; i++)
    {
        cameraName[i] << szPath << "\\camera" << i+1;
    }
    ::InitializeCriticalSection(&g_fMutex);
    HANDLE thread_handle[MAX_THREAD] = {0};
    for(int i = 0; i < MAX_THREAD; i++)
    {
        char szBuff[1024] = {0};
        memcpy(szBuff, cameraName[i].str().c_str(), strlen(cameraName[i].str().c_str()));
        thread_handle[i] = (HANDLE)_beginthreadex(NULL, 0, record_vedio, (void *)(szBuff), 0, NULL);
        WaitForSingleObjectEx(tongbu, INFINITE, TRUE);
    }
    
    WaitForMultipleObjectsEx(MAX_THREAD, thread_handle, TRUE, INFINITE, TRUE);
    
    DWORD errornum = GetLastError();
    cout << errornum << endl;
    cout << "hello 111" << endl;
    //getchar();

    for(int i = 0; i < MAX_THREAD; i++)
    {
        CloseHandle(thread_handle[i]);
        thread_handle[i] = 0;
    }
    
    ::DeleteCriticalSection(&g_fMutex);
    CloseHandle(tongbu);
    getchar();
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: