| 143 | } |
| 144 | |
| 145 | DWORD WINAPI WaitingThread(LPVOID lParam) |
| 146 | { |
| 147 | std::auto_ptr<WaitingThreadData> pThread((WaitingThreadData *)lParam); |
| 148 | WaitingThreadData &thread = *pThread; |
| 149 | std::vector<HANDLE> handles; |
| 150 | |
| 151 | while (true) |
| 152 | { |
| 153 | thread.waitingToWait = true; |
| 154 | ::WaitForSingleObject(thread.startWaiting, INFINITE); |
| 155 | thread.waitingToWait = false; |
| 156 | |
| 157 | if (thread.terminate) |
| 158 | break; |
| 159 | if (!thread.count) |
| 160 | continue; |
| 161 | |
| 162 | handles.resize(thread.count + 1); |
| 163 | handles[0] = thread.stopWaiting; |
| 164 | std::copy(thread.waitHandles, thread.waitHandles+thread.count, handles.begin()+1); |
| 165 | |
| 166 | DWORD result = ::WaitForMultipleObjects((DWORD)handles.size(), &handles[0], FALSE, INFINITE); |
| 167 | |
| 168 | if (result == WAIT_OBJECT_0) |
| 169 | continue; // another thread finished waiting first, so do nothing |
| 170 | SetEvent(thread.stopWaiting); |
| 171 | if (!(result > WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + handles.size())) |
| 172 | { |
| 173 | assert(!"error in WaitingThread"); // break here so we can see which thread has an error |
| 174 | *thread.error = ::GetLastError(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return S_OK; // return a value here to avoid compiler warning |
| 179 | } |
| 180 | |
| 181 | void WaitObjectContainer::CreateThreads(unsigned int count) |
| 182 | { |