WaitForMultipleObjects返回0xffffffff
DWORD ret;
const int THREAD_NUM = ;
HANDLE handle[THREAD_NUM]; for (int i = ; i < THREAD_NUM; i++)
handle[i] = (HANDLE)_beginthreadex(NULL, , CthreadFun, NULL, , NULL);
ret = WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);
如上写了个使用_beginthreadex的例子,发现当THREAD_NUM为100时,WaitForMultipleObjects返回的是0xffffffff。
除了什么错误呢,于是使用GetLastError()获取错误号:87,使用vs工具中的错误查找,查找对应的错误信息是"参数错误"。这是什么原因呢?
如果把THREAD_NUM改小后,就没有这个错误了,为什么呢?
假设:
1)WaitForMultipleObjects对参数ncount有大小要求?
2)系统可开的线程个数有限制?
3)线程太多,占用的内存资源多?
使用排除法进行分析:
关于3),_beginthreadex的第二个参数表示堆栈大小,以字节为单位,0默认1M。 100个线程才100M,况且查看内存使用量,没有太多呀!
关于2),msdn原文:
“The number of threads a process can create is limited by the available virtual memory. By default, every thread has one megabyte of stack space. Therefore, you can create at most 2,028 threads. If you reduce the default stack size, you can create more threads. However, your application will have better performance if you create one thread per processor and build queues of requests for which the application maintains the context information. A thread would process all requests in a queue before processing requests in the next queue.”
参考文章:http://www.cnblogs.com/lancidie/archive/2011/12/15/2289337.html 理论上2048个线程数限制。可以通过改小栈的大小增加,默认虽然实际可能达不到2048,但是100个应该可以吧?
并且,如果是2,3的问题,那么在_beginthreadex时应该失败的呀,检查句柄数组handle, 均分配成功,因此排除2,3。
最后关于1)WaitForMultipleObjects的参数范围,参考msdn原文:
DWORD WINAPI WaitForMultipleObjects(
_In_ DWORD nCount,
_In_ const HANDLE *lpHandles,
_In_ BOOL bWaitAll,
_In_ DWORD dwMilliseconds
);Parameters
- nCount [in]
The number of object handles in the array pointed to by lpHandles. The maximum number of object handles is MAXIMUM_WAIT_OBJECTS. This parameter cannot be zero.
- lpHandles [in]
An array of object handles. For a list of the object types whose handles can be specified, see the following Remarks section. The array can contain handles to objects of different types. It may not contain multiple copies of the same handle.
If one of these handles is closed while the wait is still pending, the function's behavior is undefined.
The handles must have the SYNCHRONIZE access right. For more information, see Standard Access Rights.
- bWaitAll [in]
If this parameter is TRUE, the function returns when the state of all objects in the lpHandles array is signaled. If FALSE, the function returns when the state of any one of the objects is set to signaled. In the latter case, the return value indicates the object whose state caused the function to return.
- dwMilliseconds [in]
The time-out interval, in milliseconds. If a nonzero value is specified, the function waits until the specified objects are signaled or the interval elapses. If dwMilliseconds is zero, the function does not enter a wait state if the specified objects are not signaled; it always returns immediately. If dwMilliseconds is INFINITE, the function will return only when the specified objects are signaled.
Return value
If the function succeeds, the return value indicates the event that caused the function to return. It can be one of the following values. (Note that WAIT_OBJECT_0 is defined as 0 and WAIT_ABANDONED_0 is defined as 0x00000080L.)
Return code/value Description
- WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount– 1)
If bWaitAll is TRUE, the return value indicates that the state of all specified objects is signaled.
If bWaitAll is FALSE, the return value minus WAIT_OBJECT_0 indicates the lpHandles array index of the object that satisfied the wait. If more than one object became signaled during the call, this is the array index of the signaled object with the smallest index value of all the signaled objects.
- WAIT_ABANDONED_0 to (WAIT_ABANDONED_0 + nCount– 1)
If bWaitAll is TRUE, the return value indicates that the state of all specified objects is signaled and at least one of the objects is an abandoned mutex object.
If bWaitAll is FALSE, the return value minus WAIT_ABANDONED_0 indicates the lpHandles array index of an abandoned mutex object that satisfied the wait. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled.
If a mutex was protecting persistent state information, you should check it for consistency.
- WAIT_TIMEOUT
- 0x00000102L
The time-out interval elapsed and the conditions specified by the bWaitAll parameter are not satisfied.
- WAIT_FAILED
- (DWORD)0xFFFFFFFF
The function has failed. To get extended error information, call GetLastError
其中MAXIMUM_WAIT_OBJECTS的值为.这就可以解释了错误号87的问题了。其实解决问题时,绕了个弯,87就是参数错误的意思,当然要首先从出错的函数参数方面找原因,因为自己主观首先臆测了参数范围无限制,导致了浪费时间。
注意以后解决问题要严谨,不可自我感觉,必须有凭有据。
WaitForMultipleObjects返回0xffffffff的更多相关文章
- WaitForMultipleObjects返回失败原因之一
上网搜了下 关于 WaitForMultipleObjects等待多个线程退出的状态失败的情况,也有人遇到类似的情况. 一次项目中我也遇到这么个情况.项目中创建线程都是用的 _beginthread ...
- delphi 多线程3
多线程程序设计 我们知道,win95或winNT都是“多线程”的操作系统,在DELPHI .中,我们可以充分利用这一特性,编写出“多线程”的应用程序. 对以往在DOS或16位windows下写程序的 ...
- Delphi多线程详解
(整理自网络) Delphi多线程处理 1-1多线程的基本概念 WIN 98/NT/2000/XP 是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU 运行时间和资源,或者 ...
- 整数压缩编码 ZigZag
在分析Avro源码时,发现Avro为了对int.long类型数据压缩,采用Protocol Buffers的ZigZag编码(Thrift也采用了ZigZag来压缩整数). 1. 补码编码 为了便于后 ...
- SQLLocalDB 11.0持续报错,这是泥马德什么问题啊!!!
Windows API 调用 WaitForMultipleObjects 返回了错误代码: 575.Windows 系统错误消息为: {应用程序错误}应用程序无法正常启动(0x%lx).请单击“确定 ...
- windows核心编程---第六章 线程的调度
每个线程都有一个CONTEXT结构,保存在线程内核对象中.大约每隔20ms windows就会查看所有当前存在的线程内核对象.并在可调度的线程内核对象中选择一个,将其保存在CONTEXT结构的值载入c ...
- delphi的多线程编程
多线程的基本概念 win 98/nt/2000/xp 是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用cpu 运行时间和资源,或者说,把cpu 时间划成片,每个片分给不同的线程 ...
- AcceptEx编辑
Windows套接字AcceptEx函数接受一个新的连接,返回本地和远程地址,并接收由客户端应用程序发送的第一块数据.Windows 95/98不支持AcceptEx函数. 平台SDK:Windows ...
- MFC菜单、工具栏和状态栏
菜单:CMenu类 CMenu类的主要成员函数 BOOL LoadMenu(UINT nIDResource); 加载菜单资源,并将其附加到CMenu对象上.参数nIDResource指定了要加载的菜 ...
随机推荐
- xgboost应用
在业务中,我们经常需要对数据建模并预测.简单的情况下,我们采用 if else 判断(一棵树)即可.但如果预测结果与众多因素有关,而每一个特征的权重又不尽相同. 所以我们如何把这些特征的权重合理的找出 ...
- js中数组相关的Api
话不多说,直接上图,一眼便知道怎么回事!!! forEach every some sort map filter
- Hive记录-加载文件进行查询操作
Hive可以运行保存在文件里面的一条或多条的语句,只要用-f参数,一般情况下, 保存这些Hive查询语句的文件通常用.q或者.hql后缀名,但是这不是必须的, 你也可以保存你想要的后缀名.假设test ...
- img格式镜像转ISO格式
在做汇编学习时,需要用比较老的Windows XP来进行调试学习,因此找了最老的Windows XP(CN_WINXP_PRO_ISO,无SP版本 ),下载后发现镜像文件格式是img的,而virtua ...
- jquery选择器最后一个,倒数第二个元素
<div> <p>1</p> <p>2</p> <p>3</p> <p>4</p> < ...
- 从Nexus私服下载和上传资源(一)
从私服中下载资源 首先要明确将资源下载到哪里 找到maven 配置文件settings.xml 文件,添加如下配置:1.添加镜像配置:将所有访问外网仓库的请求指向私服: <mirror> ...
- Java导出txt模板——(一)
导出txt文件时候\r\n才能换行 java代码 package DRDCWordTemplates; import java.io.BufferedWriter; import java.io.Fi ...
- ubuntu下安装pdf编辑器Master PDF Editor
在 ubuntu 上看一些 pdf 文档,自带的pdf阅读器不带编辑功能.推荐使用 master pdf editor 1. 安装QT sudo apt-get install qt-sdk 2. 下 ...
- ASP.NET MVC + EF 更新的几种方式(超赞)
1.常用 db.Entry(实体).State = EntityState.Modified;db.SaveChanges(); 2.指定更新 db.Configuration.ValidateOnS ...
- Linux内存管理4---虚拟地址空间管理
1.前言 本文所述关于内存管理的系列文章主要是对陈莉君老师所讲述的内存管理知识讲座的整理. 本讲座主要分三个主题展开对内存管理进行讲解:内存管理的硬件基础.虚拟地址空间的管理.物理地址空间的管理. 本 ...