From 22d1d4178a9bf79cab6d7383bbac01cfc69512dd Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Wed, 17 Nov 2021 17:07:34 +0800 Subject: [PATCH] =?UTF-8?q?thread=5Fsafe=20=E6=B7=BB=E5=8A=A0=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- general/src/threadsafe/thread_safe_list.hpp | 14 +++++++++----- test/src/cpp11/cpp11_test.cpp | 12 ++++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/general/src/threadsafe/thread_safe_list.hpp b/general/src/threadsafe/thread_safe_list.hpp index 198e5ed..47f8615 100644 --- a/general/src/threadsafe/thread_safe_list.hpp +++ b/general/src/threadsafe/thread_safe_list.hpp @@ -1,7 +1,7 @@ /* * @Author: caiyuzheng * @Date: 2021-10-06 23:05:26 - * @LastEditTime: 2021-11-17 15:58:26 + * @LastEditTime: 2021-11-17 17:06:03 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \generallib\general\src\threadsafe\thread_safe_list.h @@ -24,9 +24,12 @@ public: ~ThreadSafeList() { } + ThreadSafeList(ThreadSafeList &b){ + this->m_data = b.m_data; + } - int - PushBack(T v) + + int PushBack(T v) { m_mux.lock(); m_data.push_back(v); @@ -38,6 +41,7 @@ public: m_mux.lock(); m_data.push_front(v); m_mux.unlock(); + m_cv.notify_one(); return 0; } int Emplace(int pos, T v) @@ -94,11 +98,11 @@ public: m_mux.unlock(); return ret1; }else{ - m_cv.wait(&m_mux); + std::unique_lock guard(m_mux); + m_cv.wait(guard); auto ret = m_data.begin(); auto ret1 = *ret; m_data.erase(ret); - m_mux.unlock(); return ret1; } } diff --git a/test/src/cpp11/cpp11_test.cpp b/test/src/cpp11/cpp11_test.cpp index 84d3ca1..b12bbbc 100644 --- a/test/src/cpp11/cpp11_test.cpp +++ b/test/src/cpp11/cpp11_test.cpp @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-03-15 23:07:25 - * @LastEditTime: 2021-11-17 15:58:56 + * @LastEditTime: 2021-11-17 17:05:44 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \cpp11\cpp11_test.cpp @@ -30,15 +30,23 @@ extern "C"{ int main(int argc, char **argv) { - + int i = 0; ThreadSafeList ip; ip.PushFront(102); ip.PushFront(111); + void *p = &ip; + std::thread t1([p]{ + Sleep(4000); + ((ThreadSafeList*)p)->PushFront(123); + }); + std::cout<