C++ 不同线程实现不同周期的调度

通过timed_mutex中try_lock_for的方法实现不同频率的调度方式

#include <iostream>       // std::cout
#include <chrono>         // std::chrono::milliseconds
#include <thread>         // std::thread
#include <mutex>          // std::timed_mutex


std::timed_mutex mtx;
int count = 0;


void counttime(int t) {
  // waiting to get a lock: each thread change count value every t ms:
  while (true){
    // try_lock_for will let a thread lock every t ms;
    // every t ms, a thread can get the lock once;
    // when it release the lock, the lock will be competed by all the thread;
    if (!mtx.try_lock_for(std::chrono::milliseconds(t))){
        // some operations by all the threads
        count =  t;
        std::cout<< count <<std::endl;
        // release the lock when its finishing
        mtx.unlock();
    }
    
  }
  
}

int main ()
{
    // 1. decleare threads
    std::thread threads[2];
  
    // 2. let two threads with different recall time
    threads[0] = std::thread(counttime,100); // lock every 100ms
    threads[1] = std::thread(counttime,10); // lock every 10ms
    // 3. join the thread if finished
    for (auto& th : threads) th.join();

    return 0;
}

在这里插入图片描述
上述代码通过thread+互斥锁的方式,将count量变周期的更改。
很大情况下,我们用不同线程操作同一个结构体或者地址,但是对于里面更改的内容是不一样的。
如下,我们定义了一个结构体,我们希望结构体里面的a成员能100ms更新一次,b成员能10ms更新一次,同时,他们的更新可能是不同方式的,所以不同的thread需要传入不同函数指针到里面去执行不同的更新策略;

#include <iostream>       // std::cout
#include <chrono>         // std::chrono::milliseconds
#include <thread>         // std::thread
#include <mutex>          // std::timed_mutex


std::timed_mutex mtx;
int count = 0;
// declare a struct
struct TestStruct{
    int a ;
    int b ;
};
// define a struct and initilize with init list
struct TestStruct countstruct = {0,0};


// change stuct.a
void ChaneStructa(TestStruct* countstruct){
    countstruct->a = countstruct->a +1;
}

// change stuct.a
void ChaneStructb(TestStruct* countstruct){
    countstruct->b = countstruct->b +1;
}
/*
void CountTime(int t) {
  // waiting to get a lock: each thread change count value every t ms:
  while (true){
    // try_lock_for will let a thread lock every t ms;
    // every t ms, a thread can get the lock once;
    // when it release the lock, the lock will be competed by all the thread;
    if (!mtx.try_lock_for(std::chrono::milliseconds(t))){
        // some operations by all the threads
        count =  t;
        std::cout<< count <<"  ";
        // release the lock when its finishing
        mtx.unlock();
    }
    
  }
  
}
*/

// ChangeStructValue
void ChangeStructValue(int t, void (*func)(TestStruct*), TestStruct *countstruct) {
  // waiting to get a lock: each thread change count value every t ms:
  while (true){
    // try_lock_for will let a thread lock every t ms;
    // every t ms, a thread can get the lock once;
    // when it release the lock, the lock will be competed by all the thread;
    if (!mtx.try_lock_for(std::chrono::milliseconds(t))){
        // some operations by all the threads with func ptr
         (*func)(countstruct);
        std::cout<< "a = "<< countstruct->a << "; "<<"b = "<< countstruct->b <<std::endl;
        // release the lock when its finishing
        mtx.unlock();
    }
    
  }
  
}

int main ()
{
    // 1. decleare threads
    std::thread threads[2];
  
    // 2. let two threads with different recall time
    threads[0] = std::thread(ChangeStructValue, 100, ChaneStructa, &countstruct); // lock every 100ms, update a
    threads[1] = std::thread(ChangeStructValue, 10, ChaneStructb, &countstruct); // lock every 10ms, update b
    // 3. join the thread if finished
    for (auto& th : threads) th.join();

    return 0;
}

输出结果如下所示,实现了我们的需求
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值