WaitForMultipleObjects 超过 64个handle

DWORD WaitForMultipleObjects(DWORD count, const HANDLE* pHandles, DWORD millisecs)
{
	DWORD retval = WAIT_TIMEOUT;

	// Check if objects need to be split up. In theory, the maximum is
	// MAXIMUM_WAIT_OBJECTS, but I found this code performs slightly faster
	// if the object are broken down in batches smaller than this.
	if (count > 25)
	{
		// loop continuously if infinite timeout specified
		do
		{
			// divide the batch of handles in two halves ...
			DWORD split = count / 2;
			DWORD wait = (millisecs == INFINITE ? 2000 : millisecs) / 2;
			int random = rand();

			// ... and recurse down both branches in pseudo random order
			for (short branch = 0; branch < 2 && retval == WAIT_TIMEOUT; branch++)
			{
				if (random % 2 == branch)
				{
					// recurse the lower half
					retval = testWindows::WaitForMultipleObjects(split, pHandles, wait);
				}
				else
				{
					// recurse the upper half
					retval = testWindows::WaitForMultipleObjects(count - split, pHandles + split, wait);
					if (retval >= WAIT_OBJECT_0 && retval < WAIT_OBJECT_0 + split) retval += split;
				}
			}
		} while (millisecs == INFINITE && retval == WAIT_TIMEOUT);
	}
	else
	{
		// call the native win32 interface
		retval = ::WaitForMultipleObjects(count, pHandles, FALSE, millisecs);
	}

	// done
	return (retval);
}

猜你喜欢

转载自blog.csdn.net/chao56789/article/details/103975549