From f7a18ec4494b9c5c9ef3fd440bbf68ffc6425e18 Mon Sep 17 00:00:00 2001 From: yy1717 <fctom1215@outlook.com> Date: 星期四, 08 十二月 2022 15:40:56 +0800 Subject: [PATCH] 智慧驾培首次提交 --- lib/src/main/cpp/master/comm_if.cpp | 30 +++++++++++++----------------- 1 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/src/main/cpp/master/comm_if.cpp b/lib/src/main/cpp/master/comm_if.cpp index 4bed281..18a4d2f 100644 --- a/lib/src/main/cpp/master/comm_if.cpp +++ b/lib/src/main/cpp/master/comm_if.cpp @@ -21,6 +21,8 @@ #include <iostream> #include <vector> #include <list> +#include <thread> +#include <mutex> #include <semaphore.h> #include <unistd.h> @@ -94,10 +96,10 @@ static list<struct msg_2_main_t> MessageBuffer; static sem_t sem_msg_income; -static pthread_mutex_t msg_mutex = PTHREAD_MUTEX_INITIALIZER; +static std::mutex msg_mutex; static void SendMsgToMainProcIndep(int cmd, const char *value); -static void *SendMsgToMainProcThread(void *p); +static void SendMsgToMainProcThread(void); static void SendMsgToMainProcIndep(int cmd, const char *value) { @@ -111,14 +113,14 @@ msg.value.clear(); } - pthread_mutex_lock(&msg_mutex); + lock_guard<mutex> lock(msg_mutex); + MessageBuffer.push_front(msg); - pthread_mutex_unlock(&msg_mutex); sem_post(&sem_msg_income); } -static void *SendMsgToMainProcThread(void *p) { +static void SendMsgToMainProcThread(void) { while (true) { sem_wait(&sem_msg_income); @@ -126,9 +128,9 @@ struct msg_2_main_t msg; int success; - pthread_mutex_lock(&msg_mutex); + unique_lock<mutex> lock(msg_mutex); msg = MessageBuffer.back(); - pthread_mutex_unlock(&msg_mutex); + lock.unlock(); if (msg.value.length() > 0) success = SendMsgToMainProc(msg.cmd, msg.value.c_str()); @@ -136,9 +138,9 @@ success = SendMsgToMainProc(msg.cmd, NULL); if (success == 0) { - pthread_mutex_lock(&msg_mutex); + lock.lock(); MessageBuffer.pop_back(); - pthread_mutex_unlock(&msg_mutex); + lock.unlock(); } else { // 寤惰繜閲嶅彂 DEBUG("鍙戦�佸け璐�"); @@ -154,13 +156,7 @@ sem_init(&sem_msg_income, 0, 0); MessageBuffer.clear(); - pthread_mutex_init(&msg_mutex, NULL); - - pthread_t pid; - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_create(&pid, &attr, SendMsgToMainProcThread, NULL); + std::thread(SendMsgToMainProcThread).detach(); } void MA_NdkStart(void) @@ -2057,7 +2053,7 @@ { switch (cmd) { case ID_MS_FILE: { - UploadDfuFile(value, length); + ////////////////////UploadDfuFile(value, length); break; } default: -- Gitblit v1.8.0