| 48 | Conf conf; |
| 49 | template <typename T, typename S> |
| 50 | int |
| 51 | run(T &mutex) |
| 52 | { |
| 53 | std::thread list[conf.nthreads]; |
| 54 | int counter = 0; |
| 55 | |
| 56 | for (int i = 0; i < conf.nthreads; i++) { |
| 57 | new (&list[i]) std::thread{[&counter](T &mutex) { |
| 58 | int c = 0; |
| 59 | for (int j = 0; j < conf.nloop; ++j) { |
| 60 | // reader |
| 61 | for (int i = 0; i < conf.nread; ++i) { |
| 62 | S lock(mutex); |
| 63 | // Do not optimize |
| 64 | c = counter; |
| 65 | } |
| 66 | |
| 67 | // writer |
| 68 | for (int i = 0; i < conf.nwrite; ++i) { |
| 69 | std::lock_guard lock(mutex); |
| 70 | // Do not optimize |
| 71 | ++c; |
| 72 | counter = c; |
| 73 | } |
| 74 | } |
| 75 | }, |
| 76 | std::ref(mutex)}; |
| 77 | } |
| 78 | |
| 79 | for (int i = 0; i < conf.nthreads; i++) { |
| 80 | list[i].join(); |
| 81 | } |
| 82 | |
| 83 | return counter; |
| 84 | } |
| 85 | |
| 86 | } // namespace |
| 87 | |