no message

master
zcy 2021-10-08 00:51:54 +08:00
parent f43424e8c6
commit 2c9ede1eb7
2 changed files with 95 additions and 62 deletions

View File

@ -1,7 +1,7 @@
/*
* @Author: caiyuzheng
* @Date: 2021-10-06 23:05:26
* @LastEditTime: 2021-10-07 00:50:17
* @LastEditTime: 2021-10-08 00:30:49
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \generallib\general\src\threadsafe\thread_safe_list.h
@ -13,80 +13,106 @@
using namespace std;
template <class T>
class SafeList{
class ThreadSafeList
{
public:
SafeList(){
ThreadSafeList()
{
}
int PushBack(T v){
~ThreadSafeList()
{
}
int
PushBack(T v)
{
m_mux.lock();
m_data.push_back(v);
m_mux.unlock();
return 0;
}
int PushFront(T v){
int PushFront(T v)
{
m_mux.lock();
m_data.push_front(v);
m_mux.unlock();
return 0;
}
int Emplace(int pos,T v){
int Emplace(int pos, T v)
{
m_mux.lock();
auto itr =m_data.begin();
for(int i = 0;itr != m_data.end(),itr++){
i++;
if(i == pos)
auto itr = m_data.begin();
for (int i = 0; itr != m_data.end(); itr++)
{
if (i == pos)
{
m_data.emplace(itr, v);
break;
}
m_data.emplace(itr,v);
m_mux.unlock();
return 0;
}
int EmplaceEnd(T v){
m_mux.lock();
auto itr =m_data.begin();
for(int i = 0;itr != m_data.end(),itr++){
}
i++;
if(i == pos)
break;
}
m_data.emplace(itr,v);
m_mux.unlock();
return 0;
}
T PopFront(int pos,T v){
if(m_data.empty()){
int EmplaceEnd(T v)
{
m_mux.lock();
ret = m_data.front();
auto itr = m_data.begin();
for (int i = 0; itr != m_data.end(), itr++)
{
i++;
if (i == pos)
break;
}
m_data.emplace(itr, v);
m_mux.unlock();
return 0;
}
T PopFront()
{
if (!m_data.empty())
{
m_mux.lock();
auto ret = m_data.begin();
auto ret1 = *ret;
m_data.erase(ret);
m_mux.unlock();
return ret1;
}
}
T PopEnd()
{
if (!m_data.empty())
{
m_mux.lock();
auto ret = m_data.end();
m_data.erase(ret);
m_mux.unlock();
return ret;
}
}
T PopEnd(){
T ret;
if(m_data.empty()){
T Front()
{
if (!m_data.empty())
{
m_mux.lock();
ret = m_data.end();
m_data.erase(ret);
auto ret = m_data.front();
m_mux.unlock();
return ret;
}
return T();
}
uint32_t Length(){
m_mux.lock();
uint32_t Length()
{
return m_data.size();
m_mux.unlock();
}
private:
std::mutex m_mux;
std::list<T> m_data;
};

View File

@ -108,32 +108,39 @@ public:
*/
int main(){
Motherboard mon;
SoundCard soundcard("souncard",&mon);
RAM ram("ram",&mon);
mon.Register((MediatorColleague<HandleData>*) &soundcard);
mon.Register((MediatorColleague<HandleData>*) &ram);
soundcard.Send("ram",HandleData{"ss"});
// Motherboard mon;
// SoundCard soundcard("souncard",&mon);
// RAM ram("ram",&mon);
// mon.Register((MediatorColleague<HandleData>*) &soundcard);
// mon.Register((MediatorColleague<HandleData>*) &ram);
// soundcard.Send("ram",HandleData{"ss"});
BridgeTest br(new MP3Player);
BridgeTest br1(new MP4Player);
// BridgeTest br(new MP3Player);
// BridgeTest br1(new MP4Player);
br.Play();
br1.Play();
// br.Play();
// br1.Play();
Product *z = new Product;
z->SetAttr1(-11);
z->SetAttr2('c');
z->Show();
// Product *z = new Product;
// z->SetAttr1(-11);
// z->SetAttr2('c');
// z->Show();
Prototype<Product> x(z);
delete(z);
// 即使delete掉了原型也能接着使用
auto tbl3 = x.GetProduct();
tbl3->Show();
// Prototype<Product> x(z);
// delete(z);
// // 即使delete掉了原型也能接着使用
// auto tbl3 = x.GetProduct();
// tbl3->Show();
// test threadsafe
SafeList<std::string> m1;
// test threadsafe
ThreadSafeList<std::string> m1;
m1.PushBack("111");
}
auto x = m1.Front();
std::cout << x << " 1234er ";
x = m1.Front();
std::cout << x << " 1234er ";
m1.PopFront();
std::cout << m1.Length();
x = m1.Front();
std::cout << x << " 1234er1 ";
}