brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · e3e39a2 Raw
71 lines · cpp
1// This test is intended to create a situation in which one thread will be2// created while the debugger is stepping in another thread.3 4#include "pseudo_barrier.h"5#include <thread>6 7#define do_nothing()8 9pseudo_barrier_t g_barrier;10 11volatile int g_thread_created = 0;12volatile int g_test = 0;13 14void *15step_thread_func ()16{17    g_test = 0;         // Set breakpoint here18 19    while (!g_thread_created)20        g_test++;21 22    // One more time to provide a continue point23    g_test++;           // Continue from here24 25    // Return26    return NULL;27}28 29void *30create_thread_func (void *input)31{32    std::thread *step_thread = (std::thread*)input;33 34    // Wait until the main thread knows this thread is started.35    pseudo_barrier_wait(g_barrier);36 37    // Wait until the other thread is done.38    step_thread->join();39 40    // Return41    return NULL;42}43 44int main ()45{46    // Use a simple count to simulate a barrier.47    pseudo_barrier_init(g_barrier, 2);48 49    // Create a thread to hit the breakpoint.50    std::thread thread_1(step_thread_func);51 52    // Wait until the step thread is stepping53    while (g_test < 1)54        do_nothing();55 56    // Create a thread to exit while we're stepping.57    std::thread thread_2(create_thread_func, &thread_1);58 59    // Wait until that thread is started60    pseudo_barrier_wait(g_barrier);61 62    // Let the stepping thread know the other thread is there63    g_thread_created = 1;64 65    // Wait for the threads to finish.66    thread_2.join();67    thread_1.join();68 69    return 0;70}71