thread_safe 添加条件变量
parent
bb433e232d
commit
22d1d4178a
|
@ -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<T> &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<std::mutex> guard(m_mux);
|
||||
m_cv.wait(guard);
|
||||
auto ret = m_data.begin();
|
||||
auto ret1 = *ret;
|
||||
m_data.erase(ret);
|
||||
m_mux.unlock();
|
||||
return ret1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<int> ip;
|
||||
ip.PushFront(102);
|
||||
ip.PushFront(111);
|
||||
|
||||
void *p = &ip;
|
||||
std::thread t1([p]{
|
||||
Sleep(4000);
|
||||
((ThreadSafeList<int>*)p)->PushFront(123);
|
||||
});
|
||||
|
||||
std::cout<<ip.PopFrontAndWait()<<"\r\n";
|
||||
std::cout<<ip.PopFrontAndWait()<<"\r\n";
|
||||
std::cout<<ip.PopFrontAndWait()<<"\r\n";
|
||||
|
||||
|
||||
t1.join();
|
||||
// std::cout<<"test start"<<endl;
|
||||
// try{
|
||||
// std::cout<<"cpu count is "<<CoreCount()<<std::endl;
|
||||
|
|
Loading…
Reference in New Issue