...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <mutex> #include <thread> const size_t max_threadsmaxThreads = 10; void do_work(size_t i, std::mutex *pm) { std::lock_guard<std::mutex> lk(*pm); // Access data protected by the lock. } void start_threads() { std::thread threads[max_threadsmaxThreads]; std::mutex m; for (size_t i = 0; i < max_threadsmaxThreads; ++i) { threads[i] = std::thread(do_work, i, &m); } } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <mutex> #include <thread> const size_t max_threadsmaxThreads = 10; void do_work(size_t i, std::mutex *pm) { std::lock_guard<std::mutex> lk(*pm); // Access data protected by the lock. } std::mutex m; void start_threads() { std::thread threads[max_threadsmaxThreads]; for (size_t i = 0; i < max_threadsmaxThreads; ++i) { threads[i] = std::thread(do_work, i, &m); } } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <mutex> #include <thread> const size_t max_threadsmaxThreads = 10; void do_work(size_t i, std::mutex *pm) { std::lock_guard<std::mutex> lk(*pm); // Access data protected by the lock. } void run_threads() { std::thread threads[max_threadsmaxThreads]; std::mutex m; for (size_t i = 0; i < max_threadsmaxThreads; ++i) { threads[i] = std::thread(do_work, i, &m); } for (size_t i = 0; i < max_threadsmaxThreads; ++i) { threads[i].join(); } } |
...