c++ 三个线程交替打印1~100

std::condition_variable是条件变量,当 std::condition_variable对象的某个wait 函数被调用的时候,它使用 std::unique_lock(通过 std::mutex) 来锁住当前线程。当前线程会一直被阻塞,直到另外一个线程在相同的 std::condition_variable 对象上调用了 notification 函数来唤醒当前线程。

#include <thread>
#include <iostream>
#include <mutex>
#include <condition_variable>

std::mutex data_mutex;
std::condition_variable data_var;
int flag=1;
int i=0;
#define MAXNUM 100

void printA(){
    while(i<MAXNUM){
//        std::this_thread::sleep_for(std::chrono::seconds(1)); //打印节奏变慢
        std::unique_lock<std::mutex> lck(data_mutex);
        //当flag为1时,wait不阻塞;否则wait阻塞当前线程。
        //允许其他锁住的线程继续,当其他线程调用notify函数后,并且flag为1时
        //此线程被唤醒
        data_var.wait(lck, []{return flag==1;});
        flag=2;
        if(i >= 100){
            data_var.no
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要编写一个多线程程序,您需要遵循以下步骤: 1. 引入线程库:您需要引入适当的线程库,例如Java中的java.util.concurrent或C++中的pthread.h。 2. 创建线程:您需要创建线程,可以使用线程库中提供的函数来创建线程并指定线程函数。 3. 启动线程:一旦您创建了线程并指定了线程函数,您需要启动线程以开始执行线程函数。 4. 同步线程:如果您的线程需要互相同步,您需要使用锁或信号量等同步机制。 5. 终止线程:当线程完成其工作或您需要停止线程时,您需要终止线程。 下面是一个实现您描述的多线程程序的示例代码: Java代码: ```java import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class MultiThread { public static void main(String[] args) { Lock lock = new ReentrantLock(); Condition conditionA = lock.newCondition(); Condition conditionB = lock.newCondition(); Condition conditionC = lock.newCondition(); int count = 0; Thread threadA = new Thread(new Runnable() { @Override public void run() { while (count < 100) { lock.lock(); try { if (count % 10 != 0 || count == 0) { System.out.println("Thread A: " + count++); } else { conditionB.signal(); conditionA.await(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } } }); Thread threadB = new Thread(new Runnable() { @Override public void run() { while (count < 100) { lock.lock(); try { if (count % 10 != 0 || count == 0) { System.out.println("Thread B: " + count++); } else { conditionC.signal(); conditionB.await(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } } }); Thread threadC = new Thread(new Runnable() { @Override public void run() { while (count < 100) { lock.lock(); try { if (count % 10 == 0 && count != 0) { System.out.println("Thread C: " + count++); conditionA.signal(); conditionC.await(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } } }); threadA.start(); threadB.start(); threadC.start(); } } ``` 在这个例子中,我们使用了Java中的ReentrantLock和Condition来实现线程同步。我们创建了三个线程:Thread A,Thread B和Thread C。Thread A和Thread B交替打印数字,当计数到10的倍数时,Thread C来打印。 Python代码: ```python import threading lock = threading.Lock() conditionA = threading.Condition(lock) conditionB = threading.Condition(lock) conditionC = threading.Condition(lock) count = 0 def thread_a(): global count while count < 100: with lock: if count % 10 != 0 or count == 0: print("Thread A:", count) count += 1 else: conditionB.notify() conditionA.wait() def thread_b(): global count while count < 100: with lock: if count % 10 != 0 or count == 0: print("Thread B:", count) count += 1 else: conditionC.notify() conditionB.wait() def thread_c(): global count while count < 100: with lock: if count % 10 == 0 and count != 0: print("Thread C:", count) count += 1 conditionA.notify() conditionC.wait() threadA = threading.Thread(target=thread_a) threadB = threading.Thread(target=thread_b) threadC = threading.Thread(target=thread_c) threadA.start() threadB.start() threadC.start() ``` 在这个例子中,我们使用了Python中的threading库来实现多线程。我们创建了三个线程:Thread A,Thread B和Thread C。Thread A和Thread B交替打印数字,当计数到10的倍数时,Thread C来打印。我们使用了锁和条件变量来实现线程同步。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值