通过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;
}
输出结果如下所示,实现了我们的需求