[cpp]struct task { long rc; HANDLE ev; }; struct work_item { task* parent; int work; }; DWORD WINAPI func(void* p) { std::auto_ptrw ((work_item*)p); std::cout << GetTickCount() << " TASK START" << std::endl; Sleep(w->work); std::cout << GetTickCount() << " TASK END" << std::endl; if (InterlockedDecrement(&w->parent->rc) == 0) SetEvent(w->parent->ev); return 0; } int main() { size_t const work_count = 3; std::cout << GetTickCount() << " MAIN START" << std::endl; std::auto_ptr t (new task); t->rc = work_count; t->ev = CreateEvent(0, 0, 0, 0); for (size_t i = 0; i != work_count; i += 1) { work_item* w = new work_item; w->parent = t.get(); w->work = rand() % 2000 + 1000; QueueUserWorkItem(func, w, 0); } WaitForSingleObject(t->ev, INFINITE); std::cout << GetTickCount() << " MAIN END" << std::endl; } [/cpp]
[cpp]struct task { long rc; HANDLE ev; }; struct work_item { task* parent; int work; }; DWORD WINAPI func(void* p) { std::auto_ptrw ((work_item*)p); std::cout << GetTickCount() << " TASK START" << std::endl; Sleep(w->work); std::cout << GetTickCount() << " TASK END" << std::endl; if (InterlockedDecrement(&w->parent->rc) == 0) SetEvent(w->parent->ev); return 0; } int main() { size_t const work_count = 3; std::cout << GetTickCount() << " MAIN START" << std::endl; std::auto_ptr t (new task); t->rc = work_count; t->ev = CreateEvent(0, 0, 0, 0); for (size_t i = 0; i != work_count; i += 1) { work_item* w = new work_item; w->parent = t.get(); w->work = rand() % 2000 + 1000; QueueUserWorkItem(func, w, 0); } WaitForSingleObject(t->ev, INFINITE); std::cout << GetTickCount() << " MAIN END" << std::endl; } [/cpp]
[bash]#include#include #include #define _WIN32_WINNT 0x0503 struct task { long rc; HANDLE ev; }; struct work_item { task* parent; int work; }; DWORD WINAPI func(void* p) { work_item *w =((work_item*)p); std::cout << GetTickCount() << " TASK START" << std::endl; Sleep(w->work); std::cout << GetTickCount() << " TASK END" << std::endl; if (InterlockedDecrement(&w->parent->rc) == 0) SetEvent(w->parent->ev); return 0; } int main() { size_t const work_count = 3; struct task t; std::cout << GetTickCount() << " MAIN START" << std::endl; t.rc = work_count; t.ev = CreateEvent(0, 0, 0, 0); for (size_t i = 0; i != work_count; i += 1) { work_item* w = new work_item; w->parent = (struct task *)malloc(sizeof(struct task)); w->work = rand() % 2000 + 1000; QueueUserWorkItem(func, w, WT_EXECUTELONGFUNCTION); } WaitForSingleObject(t.ev, INFINITE); std::cout << GetTickCount() << "MAIN END" << std::endl; } [/bash]
For more complete information about compiler optimizations, see our Optimization Notice.