Create the threads needed for this ThreadPool. Returns an error on any error spawning the threads.
| 66 | /// Create the threads needed for this ThreadPool. Returns an error on any |
| 67 | /// error spawning the threads. |
| 68 | Status Init() { |
| 69 | for (int i = 0; i < num_threads_; ++i) { |
| 70 | std::stringstream threadname; |
| 71 | threadname << thread_prefix_ << "(" << i + 1 << ":" << num_threads_ << ")"; |
| 72 | std::unique_ptr<Thread> t; |
| 73 | Status status = Thread::Create(group_, threadname.str(), |
| 74 | boost::bind<void>(boost::mem_fn(&ThreadPool<T>::WorkerThread), this, i), &t, |
| 75 | fault_injection_eligible_); |
| 76 | if (!status.ok()) { |
| 77 | // The thread pool initialization failed. Shutdown any threads that were |
| 78 | // spawned. Note: Shutdown() and Join() are safe to call multiple times. |
| 79 | Shutdown(); |
| 80 | Join(); |
| 81 | return status; |
| 82 | } |
| 83 | threads_.AddThread(std::move(t)); |
| 84 | } |
| 85 | initialized_ = true; |
| 86 | return Status::OK(); |
| 87 | } |
| 88 | |
| 89 | /// Blocking operation that puts a work item on the queue. If the queue is full, blocks |
| 90 | /// until there is capacity available. The ThreadPool must be initialized before |