Files
2025-12-29 13:43:10 -06:00

65 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
#include <print>
#include <format>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>
std::mutex m;
std::condition_variable terminate;
void w1(int i) {
std::unique_lock<std::mutex> lock(m);
terminate.wait(lock);
std::println("finish1 thread {}", i);
}
void w2(int i) {
std::unique_lock<std::mutex> lock(m);
terminate.wait(lock);
std::println("finish2 thread {}", i);
}
void worker(int i) {
if (i & 1)
w1(i);
else
w2(i);
std::println("exit thread {}", i);
}
int main() {
std::vector<std::thread> threads;
for (int i = 0; i < 10; i++) {
threads.emplace_back(worker, i);
// TODO: add a name for a thread
}
std::string input;
std::println("allowed commands:");
std::println(" exit interrupts program.");
while(true) {
std::print("> ");
std::cin >> input;
if (input == "exit") {
terminate.notify_all();
break;
}
else {
std::println(std::cerr, "bad command: {}", input);
}
}
for (auto& thread: threads) {
thread.join();
}
}