gpt4 book ai didi

C++ 事件管理器多线程可靠吗?

转载 作者:行者123 更新时间:2023-11-30 03:25:12 29 4
gpt4 key购买 nike

我像下面这样制作类(class)事件经理事件管理器.h

#ifndef EVENTMANAGER_H
#define EVENTMANAGER_H

#include <thread>
#include <mutex>
#include <chrono>
#include <atomic>
#include <condition_variable>
#include <vector>
#include "../../object/EObject.h"

class EventManager : public EObject {
public:
EventManager();
virtual ~EventManager();

int start_event();
void stop_event();

void add(const char* name, int interval, EObject * instance);

private:
static const int MAX_EVENT = 10;
std::atomic<int> event_total;

struct {
int event_id;
std::string event_name;
int interval;
std::atomic<bool> next_execute;
EObject * instance;
std::unique_ptr<std::condition_variable> cv;
std::unique_ptr<std::mutex> mtx;
} my_event[MAX_EVENT];

std::thread * event_thread;
std::atomic<bool> shall_stop;
std::atomic<bool> has_stopped;
std::atomic<int> worker_delay;
void worker();

//timing
std::vector<std::unique_ptr<std::thread>> timing_work;
void timing(int id);
};

#endif /* EVENTMANAGER_H */

事件管理器.cpp

#include <iostream>
#include "EventManager.h"
#include "../../object/EVariant.h"


using namespace std;

EventManager::EventManager() {
event_thread = nullptr;
has_stopped = true;
shall_stop = false;
worker_delay = 5; //milli second
event_total = 0;
}

EventManager::~EventManager() {
}

int EventManager::start_event() {
if (event_thread) {
cout << "Event thread can not create\n" << flush;
return -1;
} else {
event_thread = new std::thread([this] {
this->worker();
});
cout << "Event thread created\n" << flush;
}
return 0;
}

void EventManager::stop_event() {
shall_stop = true;
for (int i = 0; i < 5; i++) {
this_thread::sleep_for(chrono::microseconds(10));

if (has_stopped) break;
}
delete event_thread;
event_thread = nullptr;
}

void EventManager::worker() {
has_stopped = false;
while (1) {
if (shall_stop) break;

for (int i = 0; i < event_total; i++) {
// cout << "Event Manager: " << my_event[i].event_name << " - checking \n" << flush;
if (my_event[i].next_execute) {
EVariant var = EVariant();
var.push("event_name", my_event[i].event_name);
my_event[i].instance->update(var);
my_event[i].next_execute = false;

{
condition_variable * cv = my_event[i].cv.get();
mutex * mtx = my_event[i].mtx.get();
unique_lock<mutex> lock(*mtx);
cv->notify_one();
// cout << "Event Manager: " << my_event[i].event_name << " - hey wakeup \n" << flush;
}
}
}
this_thread::sleep_for(chrono::milliseconds(worker_delay));
}
shall_stop = false;
has_stopped = true;
}

void EventManager::timing(int id) {
int _id = id;
cout << "Timing thread: " << my_event[_id].event_name << " - " << this_thread::get_id() << " - i was born\n" << flush;
while (1) {
int delay = my_event[_id].interval;
// cout << "Event Manager: " << my_event[_id].event_name << " - i delay \n" << flush;
this_thread::sleep_for(chrono::milliseconds(delay));

my_event[_id].next_execute = true;

{
// cout << "Event Manager: " << my_event[_id].event_name << " - i sleep \n" << flush;
condition_variable * cv = my_event[_id].cv.get();
mutex * mtx = my_event[_id].mtx.get();
unique_lock<mutex> lock(*mtx);
cv->wait(lock);
// cout << "Event Manager: " << my_event[_id].event_name << " - OK wakeup \n" << flush;
}
}
cout << "Timing thread: " << id << " - i'm quit\n" << flush;
}

void EventManager::add(const char* name, int interval, EObject* instance) {
cout << "Event adding : " << name << "\n" << flush;
event_total += 1;
int id = event_total - 1;
my_event[id].event_id = id;
my_event[id].event_name = name;
my_event[id].interval = interval;
my_event[id].instance = instance;
my_event[id].next_execute = false;

unique_ptr<mutex> mtx(new mutex());
my_event[id].mtx = std::move(mtx);

unique_ptr<condition_variable> cov(new condition_variable());
my_event[id].cv = std::move(cov);

//create thread delay
// std::thread th([this] {
// this->timing(event_total - 1);
// });

unique_ptr<thread> thd(new thread([this] {
this->timing(event_total - 1);
}));

// timing_collection.push_back(std::move(th));
timing_work.push_back(std::move(thd));
}

打电话

//event i2c communication
p_event_manager.emplace("I2C", new EventManager());
p_event_manager.at("I2C")->add("i2c_buffer", 10, pI2c.at("i2c-1"));
p_event_manager.at("I2C")->add("i2c_poll_tb", 30, p_touch_button.at("TouchButton1"));
p_event_manager.at("I2C")->add("i2c_micro_poll", 50, bsc_app);
p_event_manager.at("I2C")->start_event();

算法:当调用 add 函数时,该函数将添加结构并为延迟循环创建新线程,线程将移动到 vector,线程将更改事件标志以在事件主线程上执行下一次。

问题:问题往往是延迟的线程没有创建成功,所以事件没有被调用。如何解决?

最佳答案

在定时器函数中使用 this_thread::sleep_for() 是不可取的,你很可能会错过 sleep 周期中的触发点。对触发点的过度轮询也会导致不必要的 CPU 周期浪费。因此,您应该主要依靠在准确时间触发而无需轮询的 condition_variables。

例如,您可以推送 std::pair<time_point,thread_id>在按时间顺序排序的有序容器中。另请查看优先队列是否适合您的需求。您的例程过于复杂,简单有其自身的品质。

关于C++ 事件管理器多线程可靠吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49142574/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com