1 //===-- main.cpp ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
16 std::default_random_engine g_random_engine{std::random_device{}()};
17 std::uniform_int_distribution<> g_distribution{0, 3000000};
22 access_pool (bool flag = false)
24 static std::mutex g_access_mutex;
26 g_access_mutex.lock();
28 uint32_t old_val = g_val;
33 g_access_mutex.unlock();
38 thread_func (uint32_t thread_index)
40 // Break here to test that the stop-hook mechanism works for multiple threads.
41 printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index);
47 // random micro second sleep from zero to 3 seconds
48 int usec = g_distribution(g_random_engine);
49 printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec);
50 std::this_thread::sleep_for(std::chrono::microseconds{usec});
55 val = access_pool (true);
57 printf ("%s (thread = %u) after usleep access_pool returns %d (count=%d)...\n", __FUNCTION__, thread_index, val, count);
59 printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index);
63 int main (int argc, char const *argv[])
65 std::thread threads[3];
67 printf ("Before turning all three threads loose...\n"); // Set break point at this line, and add a stop-hook.
69 for (auto &thread : threads)
70 thread = std::thread{thread_func, std::distance(threads, &thread)};
72 // Join all of our threads
73 for (auto &thread : threads)