多线程的时候遇到的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; }
相关文章推荐
- ubuntu 15.10 安装jdk
- Linux系统中使用netcat命令的奇技淫巧
- nyoj35 表达式求值
- Android dex 分包方案
- Linux系统下强大的lsof命令使用宝典
- 罗升阳:那两年炼就的Android内功修养
- Set,List,Map的区别 Set(集) List(列表) Map(映射)
- 整个App只有一个页面是横屏(视频)的实现逻辑
- shell脚本 一键 安装 php7
- STM32学习之路-SysTick的应用(时间延迟)
- CoreText原理及基本使用方法
- Linux下针对路由功能配置iptables的方法详解
- Linux下多线程下载工具MWget和Axel使用介绍
- HDOJ2046骨牌铺方格
- map——映射(message.cpp)
- Linux中df命令查询磁盘信息和fdisk命令分区的用法
- php中echo和return的区别
- android之基于Echart的图表
- html语义化
- pip的安装