#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
using namespace std;
class Sigleton
{
public:
static Sigleton *GetInstance()
{
if (m_intance == nullptr)
{
std::unique_lock <std::mutex> lock(mutex);
if (m_intance == nullptr)
m_intance = new Sigleton;
}
m_count++;
return m_intance;
}
static void Release()
{
if (m_count > 0)
m_count--;
if (m_intance != nullptr && m_count == 0)
{
delete m_intance;
m_intance = nullptr;
}
}
private:
Sigleton()
{
std::this_thread::sleep_for(std::chrono::seconds(2));
cout << "Sigleton 构造函数" << endl;
}
static int m_count;
static Sigleton *m_intance;
static std::mutex mutex;
};
Sigleton* Sigleton::m_intance = nullptr;
int Sigleton::m_count = 0;
std::mutex Sigleton::mutex;
void func(int i=0)
{
cout << "第" << i << "个线程开启" << endl;
Sigleton* ps = Sigleton::GetInstance();
}
int main()
{
vector<std::thread> v;
for (int i = 0; i < 10; i++)
{
/*std::thread t(func, i+1);
t.detach();*/
v.push_back(std::thread(func, i + 1));
}
for (int i = 0; i < 10; i++)
v[i].join();
system("pause");
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Clothes
{
public:
virtual void show() = 0;
};
class T_shrit:public Clothes
{
public:
void show()
{
cout << "T_shrit" << endl;
}
};
class Jacket:public Clothes
{
public:
void show()
{
cout << "Jacket" << endl;
}
};
class Factory
{
public:
Clothes *CreateClothes(string name)
{
if (name == "T恤")
return new T_shrit;
if (name == "夹克")
return new Jacket;
}
};
int main()
{
Factory *pf1 = new Factory;
Clothes*pc1 = pf1->CreateClothes("夹克");
Clothes *pc2 = pf1->CreateClothes("T恤");
pc1->show();
pc2->show();
system("pause");
return 0;
}