| 101 | }; |
| 102 | |
| 103 | SC::Result SC::ThreadPool::create(size_t workerThreads) |
| 104 | { |
| 105 | SC_TRY_MSG(numWorkerThreads == 0, "Cannot create already inited threadpool"); |
| 106 | SC_TRY_MSG(workerThreads > 0, "Cannot create threadpool with 0 worker threads"); |
| 107 | |
| 108 | // Creating threads and detaching them, as they will take care themselves of monitoring the incoming tasks. |
| 109 | for (size_t idx = 0; idx < workerThreads; idx++) |
| 110 | { |
| 111 | // Not using SC::Thread to avoid needing to store Function memory |
| 112 | #if SC_PLATFORM_WINDOWS |
| 113 | DWORD threadID; |
| 114 | HANDLE thread = ::CreateThread(0, 512 * 1024, &WorkerThread::execute, this, CREATE_SUSPENDED, &threadID); |
| 115 | if (thread == nullptr) |
| 116 | { |
| 117 | return Result::Error("ThreadPool::create - CreateThread failed"); |
| 118 | } |
| 119 | ::ResumeThread(thread); |
| 120 | ::CloseHandle(thread); |
| 121 | #else |
| 122 | pthread_t thread; |
| 123 | const int res = ::pthread_create(&thread, nullptr, &WorkerThread::execute, this); |
| 124 | if (res != 0) |
| 125 | { |
| 126 | return Result::Error("ThreadPool::create - pthread_create failed"); |
| 127 | } |
| 128 | ::pthread_detach(thread); |
| 129 | #endif |
| 130 | } |
| 131 | numWorkerThreads = workerThreads; |
| 132 | return Result(true); |
| 133 | } |
| 134 | |
| 135 | SC::Result SC::ThreadPool::destroy() |
| 136 | { |