多线程支持

This commit is contained in:
JIe 2024-11-07 14:20:44 +08:00
parent 2404e17d10
commit cea5209222

View File

@ -138,6 +138,8 @@ void ComKit::Start(std::string watchingPort, std::string input_commands) {
} }
void ComKit::Work() { void ComKit::Work() {
if (isWorking) { if (isWorking) {
std::thread([]() {
std::lock_guard<std::mutex> lock(m);
auto currentPortStructures = GetAllComPortNames(); auto currentPortStructures = GetAllComPortNames();
std::vector<std::string> currentPorts; std::vector<std::string> currentPorts;
for (auto s : *currentPortStructures) { for (auto s : *currentPortStructures) {
@ -146,7 +148,8 @@ void ComKit::Work() {
for (auto &port : watchingComPort) { for (auto &port : watchingComPort) {
if (std::find(currentPorts.begin(), currentPorts.end(), port) != if (std::find(currentPorts.begin(), currentPorts.end(), port) !=
currentPorts.end()) { currentPorts.end()) {
spdlog::info(std::format("Start Work With Port [{}]", port)); spdlog::info(
std::format("Start Work With Port [{}]", port));
device.OpenDevice(port); device.OpenDevice(port);
for (auto &command : commands) { for (auto &command : commands) {
spdlog::info(std::format("Send Command: {}", command)); spdlog::info(std::format("Send Command: {}", command));
@ -158,6 +161,7 @@ void ComKit::Work() {
device.CloseDevice(); device.CloseDevice();
} }
} }
}).detach();
} }
} }