1 | // Copyright (C) 2014 Vicente Botet |
---|---|
2 | // |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
5 | |
6 | #define BOOST_THREAD_VERSION 4 |
7 | |
8 | #include <boost/thread/shared_mutex.hpp> |
9 | #include <thread> |
10 | #include <mutex> |
11 | #include <shared_mutex> |
12 | #include <atomic> |
13 | #include <vector> |
14 | |
15 | using MutexT = boost::shared_mutex; |
16 | using ReaderLockT = std::lock_guard<MutexT>; |
17 | using WriterLockT = std::shared_lock<MutexT>; |
18 | |
19 | MutexT gMutex; |
20 | std::atomic<bool> running(true); |
21 | |
22 | |
23 | void myread() |
24 | { |
25 | long reads = 0; |
26 | while (running && reads < 100000) |
27 | { |
28 | ReaderLockT lock(gMutex); |
29 | std::this_thread::yield(); |
30 | ++reads; |
31 | } |
32 | } |
33 | |
34 | int main() |
35 | { |
36 | using namespace std; |
37 | |
38 | vector<thread> threads; |
39 | for (int i = 0; i < 256; ++i) |
40 | { |
41 | threads.emplace_back(args: thread(myread)); |
42 | } |
43 | |
44 | // string str; |
45 | // |
46 | // getline(std::cin, str); |
47 | running = false; |
48 | |
49 | for (auto& thread : threads) |
50 | { |
51 | thread.join(); |
52 | } |
53 | |
54 | return 0; |
55 | } |
56 | |
57 |