3 Star 10 Fork 6

secondtonone1/cpplearn

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
singleton_.cpp 748 Bytes
一键复制 编辑 原始数据 按行查看 历史
secondtonone1 提交于 2022-01-26 17:47 +08:00 . master
#include "../inc/singleton_.h"
#include <thread>
shared_ptr<Singleton_> Singleton_::_inst = nullptr;
mutex Singleton_::_mutex;
void test_single()
{
shared_ptr<Singleton_> inst1 = Singleton_::getinstance();
cout << "inst1 get ptr is " << inst1.get() << endl;
shared_ptr<Singleton_> inst2 = Singleton_::getinstance();
cout << "inst2 get ptr is " << inst2.get() << endl;
}
void thread_func(int i)
{
cout << "this is thread " << i << endl;
shared_ptr<Singleton_> inst = Singleton_::getinstance();
cout << "inst ptr is " << inst.get() << endl;
}
void test_thread_single()
{
for (int i = 0; i < 3; i++)
{
thread tid(thread_func, i + 1);
tid.join();
}
cout << "main thread exit " << endl;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/secondtonone1/cpplearn.git
git@gitee.com:secondtonone1/cpplearn.git
secondtonone1
cpplearn
cpplearn
master

搜索帮助