EnterCriticalSection 崩溃,线程超过 64 个
EnterCriticalSection crashes with more than 64 threads
是否有线程限制 EnterCriticalSection() 可以应付?以下代码适用于 64 个线程,但在使用 65 个或更多线程时崩溃:
CRITICAL_SECTION TestCritSection;
unsigned int threadId;
int getThreadId()
{
int tid = -1;
EnterCriticalSection(&TestCritSection);
tid= threadId;
threadId++;
LeaveCriticalSection(&TestCritSection);
return tid;
}
void parallelTest()
{
int tid = getThreadId();
cout << "Thread " << tid << " executed" << endl;
}
void
multiThreadTest()
{
unsigned int numThreads = 64; // fine, but program crashes when numThreads is set to 65 or more
HANDLE *threads = new HANDLE[numThreads];
DWORD ThreadID;
threadId = 1;
if (!InitializeCriticalSectionAndSpinCount(&TestCritSection, 0x00000400)) return;
for (int i=0; i<numThreads; ++i)
{
threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) parallelTest, (LPVOID) NULL, 0, &ThreadID);
}
WaitForMultipleObjects(numThreads, threads, TRUE, INFINITE);
DeleteCriticalSection(&TestCritSection);
for (int i=0; i<numThreads; ++i)
{
CloseHandle(threads[i]);
}
delete [] threads;
}
我想CRITICAL_SECTION内部使用的是最大计数为 64 的信号量。我可以以某种方式改变它吗?
由于没有人愿意回答我的问题,我将根据HansPassant对我问题的评论自己做。
他指出,问题WaitForMultipleObjects()
,它需要一个参数
n计数 [在]
The number of object handles in the array pointed to by lpHandles.
对象句柄的最大数量为 MAXIMUM_WAIT_OBJECTS。这 参数不能为零。
(来自MSDN)
MAXIMUM_WAIT_OBJECTS
定义为 64,此线程中的详细信息。
这意味着:是的,线程数量有一个硬编码限制,但限制不是EnterCriticalSection
而是WaitForMultipleObjects
,它返回我应该检查的错误代码。
下面是有关如何让超过 64 个线程并行工作的更多信息。
相关文章:
- CoInitialize()在单独的线程上崩溃而不返回
- 为什么我的多线程作业队列崩溃
- C/C++ Linux 上的多线程服务器/客户端崩溃
- ZeroMQ 在使用 std::thread 创建工作线程时崩溃
- 使用-static libstdc++时std::线程弱,因此在运行时会导致崩溃
- Qt-工作线程崩溃时将cv::Mat转换为QImage
- 在官方 ZeroMQ 多线程示例的修改版本中崩溃
- 在提升时不一致崩溃::线程::release_handle?
- C 多线程崩溃应用程序
- 在 Unity 中C++使用后台线程进行图像处理会导致崩溃
- C++线程的向量在连接时随机崩溃
- Malloc()/free()在Windows上的几个线程中崩溃
- 使用多线程崩溃
- C++多线程应用程序崩溃
- CUDA内核无缘无故地使用20K 线程崩溃
- Qopenglwidget将上下文移至另一个线程崩溃
- 使用QT和QOGL的多线程崩溃
- 复制提升::跨线程崩溃的异常
- 释放模式下的boost线程崩溃
- 退出时分离的线程崩溃