brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.2 KiB · fdc060d Raw
66 lines · cpp
1#include "pseudo_barrier.h"2#include <condition_variable>3#include <mutex>4#include <thread>5#include <vector>6 7std::mutex mutex;8std::condition_variable cond;9pseudo_barrier_t thread3_barrier;10 11void *12thread3(void *input)13{14    pseudo_barrier_wait(thread3_barrier);15 16    int dummy = 47; // thread3-before-lock17 18    std::unique_lock<std::mutex> lock(mutex); 19    cond.notify_all(); // Set thread3 break point on notify_all at this line.20    return NULL;21}22 23void *24thread2(void *input)25{26    std::unique_lock<std::mutex> lock(mutex);27    cond.notify_all(); // release main thread28    cond.wait(lock);29    return NULL;30}31 32void *33thread1(void *input)34{35    std::thread thread_2(thread2, nullptr);36    thread_2.join();37 38    return NULL;39}40 41int main()42{43    std::unique_lock<std::mutex> lock(mutex);44 45    std::thread thread_1(thread1, nullptr);46    cond.wait(lock); // wait for thread247 48    pseudo_barrier_init(thread3_barrier, 10);49 50    std::vector<std::thread> thread_3s;51    for (int i = 0; i < 10; i++) {52      thread_3s.push_back(std::thread(thread3, nullptr));53    }54 55    cond.wait(lock); // wait for thread_3s56 57    lock.unlock();58 59    thread_1.join();60    for (auto &t : thread_3s){61        t.join();62    }63 64    return 0;65}66