xref: /llvm-project/lldb/test/API/commands/thread/backtrace/main.cpp (revision 635f03fe976e250cc64999663f47716412dfa029)
1 // This test is intended to create a situation in which two threads are stopped
2 // at a breakpoint and the debugger issues a step-out command.
3 
4 #include "pseudo_barrier.h"
5 #include <thread>
6 
7 pseudo_barrier_t g_barrier;
8 
9 volatile int g_test = 0;
10 
stop_here()11 void stop_here() {
12   g_test += 5; // Set breakpoint here
13 }
14 
recurse_a_bit_1(int count)15 void recurse_a_bit_1(int count) {
16   if (count == 50)
17     stop_here();
18   else
19     recurse_a_bit_1(++count);
20 }
21 
recurse_a_bit_2(int count)22 void recurse_a_bit_2(int count) {
23   if (count == 50)
24     stop_here();
25   else
26     recurse_a_bit_2(++count);
27 }
28 
thread_func_1()29 void *thread_func_1() {
30   // Wait until both threads are running
31   pseudo_barrier_wait(g_barrier);
32 
33   // Start the recursion:
34   recurse_a_bit_1(0);
35 
36   // Return
37   return NULL;
38 }
39 
thread_func_2()40 void *thread_func_2() {
41   // Wait until both threads are running
42   pseudo_barrier_wait(g_barrier);
43 
44   // Start the recursion:
45   recurse_a_bit_2(0);
46 
47   // Return
48   return NULL;
49 }
50 
main()51 int main() {
52   // Don't let either thread do anything until they're both ready.
53   pseudo_barrier_init(g_barrier, 2);
54 
55   // Create two threads
56   std::thread thread_1(thread_func_1);
57   std::thread thread_2(thread_func_2);
58 
59   // Wait for the threads to finish
60   thread_1.join();
61   thread_2.join();
62 
63   return 0;
64 }
65