online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <thread> #include <iostream> #include <future> #include <syncstream> #include <mutex> #include <condition_variable> #include <atomic> std::mutex mtx{}; std::condition_variable cv{}; uint8_t workers_finished{ 0 }; // Counter for finished workers void log(const char* str) { std::osyncstream ss(std::cout); ss << str << std::endl; } void worker1(std::future<int> fut) { log("this is thread 1"); fut.get(); log("thread 1 exits"); std::lock_guard lock(mtx); ++workers_finished; cv.notify_one(); // Signal main thread } void worker2(std::promise<int> prom) { log("this is thread 2"); prom.set_value(10); log("thread 2 exits"); std::lock_guard lock(mtx); ++workers_finished; cv.notify_one(); // Signal main thread } int main() { std::promise<int> prom; std::future<int> fut = prom.get_future(); // Fire the 2 threads: std::thread t1(worker1, std::move(fut)); std::thread t2(worker2, std::move(prom)); t1.detach(); t2.detach(); { std::unique_lock lock(mtx); while (workers_finished < 2) { cv.wait(lock); // Wait until notified (or spurious wakeup) } } log("main exits"); }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue