3 ms·
> you can signal on an event from the receiver thread, and it doesn't wake up the waiting threads instantaneously, even if they are waiting on a kernel-level ob
by mehrdadn 6y ago
> you can signal on an event from the receiver thread, and it doesn't wake up the waiting threads instantaneously, even if they are waiting on a kernel-level object.
This isn't normally the case. You should expect this not to be the case because programs would be insanely inefficient if this happened. My suspicion is there was something else going on. For example, maybe all CPUs were busy running threads (possibly with higher priority?) and so there was no CPU a waiting worker could be scheduled on. But it's not normally what's supposed to happen; it's pretty easy to demonstrate threads get notified practically immediately and don't wait for a time slice. Just run this example and you'll see threads getting notified in a few microseconds:
#include <process.h>
#include <tchar.h>
#include <Windows.h>
LARGE_INTEGER prev_time;
unsigned int CALLBACK worker(void *handle)
{
LARGE_INTEGER pc, pf;
QueryPerformanceFrequency(&pf);
WaitForSingleObject(handle, 5000);
QueryPerformanceCounter(&pc);
_tprintf(_T("%lu us\n"), (pc.QuadPart - prev_time.QuadPart) * 1000000LL / pf.QuadPart);
return 0;
}
int _tmain(int argc, TCHAR *argv[])
{
HANDLE handle = CreateEvent(NULL, FALSE, FALSE, NULL);
uintptr_t thd = _beginthreadex(NULL, 0, worker, handle, 0, NULL);
Sleep(100);
QueryPerformanceCounter(&prev_time);
SetEvent(handle);
WaitForSingleObject((HANDLE)thd, INFINITE);
}