| 309 | } |
| 310 | |
| 311 | void vtkMultiThreader::MultipleMethodExecute() |
| 312 | { |
| 313 | int thread_loop; |
| 314 | |
| 315 | #ifdef VTK_USE_WIN32_THREADS |
| 316 | DWORD threadId; |
| 317 | HANDLE process_id[VTK_MAX_THREADS] = {}; |
| 318 | #endif |
| 319 | |
| 320 | #ifdef VTK_USE_PTHREADS |
| 321 | pthread_t process_id[VTK_MAX_THREADS] = {}; |
| 322 | #endif |
| 323 | |
| 324 | // obey the global maximum number of threads limit |
| 325 | if (vtkMultiThreaderGlobalMaximumNumberOfThreads && |
| 326 | this->NumberOfThreads > vtkMultiThreaderGlobalMaximumNumberOfThreads) |
| 327 | { |
| 328 | this->NumberOfThreads = vtkMultiThreaderGlobalMaximumNumberOfThreads; |
| 329 | } |
| 330 | |
| 331 | for (thread_loop = 0; thread_loop < this->NumberOfThreads; thread_loop++) |
| 332 | { |
| 333 | if (this->MultipleMethod[thread_loop] == (vtkThreadFunctionType) nullptr) |
| 334 | { |
| 335 | vtkErrorMacro(<< "No multiple method set for: " << thread_loop); |
| 336 | return; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | #ifdef VTK_USE_WIN32_THREADS |
| 341 | // Using CreateThread on Windows |
| 342 | // |
| 343 | // We want to use CreateThread to start this->NumberOfThreads - 1 |
| 344 | // additional threads which will be used to call the NumberOfThreads-1 |
| 345 | // methods defined in this->MultipleMethods[](). The parent thread |
| 346 | // will call this->MultipleMethods[NumberOfThreads-1](). When it is done, |
| 347 | // it will wait for all the children to finish. |
| 348 | // |
| 349 | // First, start up the this->NumberOfThreads-1 processes. Keep track |
| 350 | // of their process ids for use later in the waitid call |
| 351 | for (thread_loop = 1; thread_loop < this->NumberOfThreads; thread_loop++) |
| 352 | { |
| 353 | this->ThreadInfoArray[thread_loop].UserData = this->MultipleData[thread_loop]; |
| 354 | this->ThreadInfoArray[thread_loop].NumberOfThreads = this->NumberOfThreads; |
| 355 | process_id[thread_loop] = CreateThread(nullptr, 0, this->MultipleMethod[thread_loop], |
| 356 | ((void*)(&this->ThreadInfoArray[thread_loop])), 0, &threadId); |
| 357 | if (process_id[thread_loop] == nullptr) |
| 358 | { |
| 359 | vtkErrorMacro("Error in thread creation !!!"); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Now, the parent thread calls the last method itself |
| 364 | this->ThreadInfoArray[0].UserData = this->MultipleData[0]; |
| 365 | this->ThreadInfoArray[0].NumberOfThreads = this->NumberOfThreads; |
| 366 | (this->MultipleMethod[0])((void*)(&this->ThreadInfoArray[0])); |
| 367 | |
| 368 | // The parent thread has finished its method - so now it |