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