C++11的一种多线程安全的单例模式

#include
#include
using namespace std;
class Test
{
public:
Test()
{
cout << “create Test class” << endl;
num++;
cout << “create Test class finish” << endl;
}
void ttt()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
cout << "this->num = " }
static int num;
};
int Test::num = 0;
void display(int i)
{
std::this_thread::sleep_for(std::chrono::seconds(5));
cout << i << " " << this_thread::get_id() << endl;
static Test t1;
t1.ttt();
}
int main()
{
thread th1[10];
for (int i = 0; i < 10; i++)
{
th1[i] = std::thread(display, i);
}
for (int i = 0; i < 10; i++)
{
th1[i].detach();
}
system(“pause”);
return 0;
}
运行结果:
C++11的一种多线程安全的单例模式_第1张图片

你可能感兴趣的:(C++)