1.概要
c++激活指定的线程交叉对比试验
- 两个线程等待,两个线程都激活
- 两个线程等待,第二个线程先激活
- 两个线程等待,第一个线程先激活
2.代码
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic> namespace test1 {std::mutex mtx;std::condition_variable cv;std::atomic_bool ready(false);std::mutex mtx2;std::condition_variable cv2;std::atomic_bool ready2(false);void worker_thread() {std::unique_lock<std::mutex> lock(mtx);cv.wait(lock, []() { return ready.load(); }); // 等待直到ready为true // 线程被唤醒后,继续执行后续操作 std::cout << "Worker thread is awaken and continuing execution.----------1\n";}void worker_thread2() {std::unique_lock<std::mutex> lock(mtx2);cv2.wait(lock, []() { return ready2.load(); }); // 等待直到ready为true // 线程被唤醒后,继续执行后续操作 std::cout << "Worker thread is awaken and continuing execution.---------2\n";}void main() {std::thread t(worker_thread);std::thread t2(worker_thread2);// 模拟一些工作,然后唤醒线程 std::this_thread::sleep_for(std::chrono::seconds(2));{std::lock_guard<std::mutex> lock(mtx);ready.store(true); // 设置ready为true,这可能会唤醒等待的线程 std::lock_guard<std::mutex> lock2(mtx2);ready2.store(true); // 设置ready为true,这可能会唤醒等待的线程 }cv.notify_one(); // 唤醒一个等待在cv上的线程(如果有的话) cv2.notify_one(); // 唤醒一个等待在cv上的线程(如果有的话) t.join();t2.join();}
}
//单独唤醒线程2
namespace test2 {std::mutex mtx;std::condition_variable cv;std::atomic_bool ready(false);std::mutex mtx2;std::condition_variable cv2;std::atomic_bool ready2(false);void worker_thread() {std::unique_lock<std::mutex> lock(mtx);cv.wait(lock, []() { return ready.load(); }); // 等待直到ready为true // 线程被唤醒后,继续执行后续操作 std::cout << "Worker thread is awaken and continuing execution.----------1\n";}void worker_thread2() {std::unique_lock<std::mutex> lock(mtx2);cv2.wait(lock, []() { return ready2.load(); }); // 等待直到ready为true // 线程被唤醒后,继续执行后续操作 std::cout << "Worker thread is awaken and continuing execution.---------2\n";std::cout << "WWait 2 minutes before waking up thread -------1\n";std::this_thread::sleep_for(std::chrono::seconds(2));cv.notify_one();}void main() {std::cout << "Wake thread 2 alone\n";std::thread t(worker_thread);std::thread t2(worker_thread2);// 模拟一些工作,然后唤醒线程 std::this_thread::sleep_for(std::chrono::seconds(2));{std::lock_guard<std::mutex> lock(mtx);ready.store(true); // 设置ready为true,这可能会唤醒等待的线程 std::lock_guard<std::mutex> lock2(mtx2);ready2.store(true); // 设置ready为true,这可能会唤醒等待的线程 }//cv.notify_one(); // 唤醒一个等待在cv上的线程(如果有的话) cv2.notify_one(); // 唤醒一个等待在cv上的线程(如果有的话) t.join();t2.join();}
}
//单独唤醒线程1
namespace test3 {std::mutex mtx;std::condition_variable cv;std::atomic_bool ready(false);std::mutex mtx2;std::condition_variable cv2;std::atomic_bool ready2(false);void worker_thread() {std::unique_lock<std::mutex> lock(mtx);cv.wait(lock, []() { return ready.load(); }); // 等待直到ready为true // 线程被唤醒后,继续执行后续操作 std::cout << "Worker thread is awaken and continuing execution.----------1\n";std::cout << "WWait 2 minutes before waking up thread --------2\n";std::this_thread::sleep_for(std::chrono::seconds(2));cv2.notify_one();}void worker_thread2() {std::unique_lock<std::mutex> lock(mtx2);cv2.wait(lock, []() { return ready2.load(); }); // 等待直到ready为true // 线程被唤醒后,继续执行后续操作 std::cout << "Worker thread is awaken and continuing execution.---------2\n";}void main() {std::cout << "Wake thread 1 alone\n";std::thread t(worker_thread);std::thread t2(worker_thread2);// 模拟一些工作,然后唤醒线程 std::this_thread::sleep_for(std::chrono::seconds(2));{std::lock_guard<std::mutex> lock(mtx);ready.store(true); // 设置ready为true,这可能会唤醒等待的线程 std::lock_guard<std::mutex> lock2(mtx2);ready2.store(true); // 设置ready为true,这可能会唤醒等待的线程 }cv.notify_one(); // 唤醒一个等待在cv上的线程(如果有的话) //cv2.notify_one(); // 唤醒一个等待在cv上的线程(如果有的话) t.join();t2.join();}
}int main() {test1::main();test2::main();test3::main();return 0;
}
3.运行结果
Worker thread is awaken and continuing execution.----------1
Worker thread is awaken and continuing execution.---------2
Wake thread 2 alone
Worker thread is awaken and continuing execution.---------2
WWait 2 minutes before waking up thread -------1
Worker thread is awaken and continuing execution.----------1
Wake thread 1 alone
Worker thread is awaken and continuing execution.----------1
WWait 2 minutes before waking up thread --------2
Worker thread is awaken and continuing execution.---------2