- SetEvent(ioMan->hExitEvent);
- // ToDo: we can't free this now, because the worker thread(s)
- // haven't necessarily finished with it yet. Perhaps it should
- // have a reference count or something.
- // free(ioMan);
- // ioMan = NULL;
+ int num;
+
+ SetEvent(ioMan->hExitEvent);
+
+ if (wait_threads) {
+ /* Wait for all worker threads to die. */
+ for (;;) {
+ EnterCriticalSection(&ioMan->manLock);
+ num = ioMan->numWorkers;
+ LeaveCriticalSection(&ioMan->manLock);
+ if (num == 0)
+ break;
+ Sleep(10);
+ }
+ FreeWorkQueue(ioMan->workQueue);
+ CloseHandle(ioMan->hExitEvent);
+ DeleteCriticalSection(&ioMan->active_work_lock);
+ DeleteCriticalSection(&ioMan->manLock);
+ free(ioMan);
+ ioMan = NULL;
+ }