1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| // unique_lock::try_lock example #include <iostream> // std::cout #include <vector> // std::vector #include <thread> // std::thread #include <mutex> // std::mutex, std::unique_lock, std::defer_lock std::mutex mtx; // mutex for critical section void print_star () { std::unique_lock<std::mutex> lck(mtx,std::defer_lock); // print '*' if successfully locked, 'x' otherwise: if (lck.try_lock()) std::cout << '*'; else std::cout << 'x'; } int main () { std::vector<std::thread> threads; for (int i=0; i<500; ++i) threads.emplace_back(print_star); for (auto& x: threads) x.join(); return 0; }
|