xref: /llvm-project/lldb/test/API/functionalities/thread/multi_break/main.cpp (revision fdea9a4ec9b0d9585b8fe8a612686d9f44f40ddc)
1*99451b44SJordan Rupprecht // This test is intended to create a situation in which a breakpoint will be
2*99451b44SJordan Rupprecht // hit in two threads at nearly the same moment.  The expected result is that
3*99451b44SJordan Rupprecht // the breakpoint in the second thread will be hit while the breakpoint handler
4*99451b44SJordan Rupprecht // in the first thread is trying to stop all threads.
5*99451b44SJordan Rupprecht 
6*99451b44SJordan Rupprecht #include "pseudo_barrier.h"
7*99451b44SJordan Rupprecht #include <thread>
8*99451b44SJordan Rupprecht 
9*99451b44SJordan Rupprecht pseudo_barrier_t g_barrier;
10*99451b44SJordan Rupprecht 
11*99451b44SJordan Rupprecht volatile int g_test = 0;
12*99451b44SJordan Rupprecht 
13*99451b44SJordan Rupprecht void *
thread_func()14*99451b44SJordan Rupprecht thread_func ()
15*99451b44SJordan Rupprecht {
16*99451b44SJordan Rupprecht     // Wait until both threads are running
17*99451b44SJordan Rupprecht     pseudo_barrier_wait(g_barrier);
18*99451b44SJordan Rupprecht 
19*99451b44SJordan Rupprecht     // Do something
20*99451b44SJordan Rupprecht     g_test++;       // Set breakpoint here
21*99451b44SJordan Rupprecht 
22*99451b44SJordan Rupprecht     // Return
23*99451b44SJordan Rupprecht     return NULL;
24*99451b44SJordan Rupprecht }
25*99451b44SJordan Rupprecht 
main()26*99451b44SJordan Rupprecht int main ()
27*99451b44SJordan Rupprecht {
28*99451b44SJordan Rupprecht     // Don't let either thread do anything until they're both ready.
29*99451b44SJordan Rupprecht     pseudo_barrier_init(g_barrier, 2);
30*99451b44SJordan Rupprecht 
31*99451b44SJordan Rupprecht     // Create two threads
32*99451b44SJordan Rupprecht     std::thread thread_1(thread_func);
33*99451b44SJordan Rupprecht     std::thread thread_2(thread_func);
34*99451b44SJordan Rupprecht 
35*99451b44SJordan Rupprecht     // Wait for the threads to finish
36*99451b44SJordan Rupprecht     thread_1.join();
37*99451b44SJordan Rupprecht     thread_2.join();
38*99451b44SJordan Rupprecht 
39*99451b44SJordan Rupprecht     return 0;
40*99451b44SJordan Rupprecht }
41