// C11multithread.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。//#include <iostream>#include<thread>#include<string>#include<map>#include<vector>#include<list>#include<mutex>using namespace std;std::mutex resource_mutex;std::once_flag g_flag; //这是个系统定义的标记//单例设计模式class MyCAS { static void CreateInstance() { //只被调用一次的函数 std::chrono::milliseconds dura(2000); //休息20秒 std::this_thread::sleep_for(dura); cout << "call_once()执行完毕" << endl; m_instance = new MyCAS; static CGarhuishou cl; //静态类成员对象 程序退出时调用析构函数 }private: MyCAS() { //私有化构造对象 }private: static MyCAS *m_instance; //静态成员变量 public: static MyCAS *GetInstance() { //提高效率: //如果if(m_instance!=NULL)条件成立,则肯定表示m_instance已经被new过了 //如果if(m_instance==NULL),不代表m_instance一定被new过了。 //if (m_instance == NULL) { //双重锁定(双重检查) // std::unique_lock<std::mutex> myobj(resource_mutex); // if (m_instance == NULL) { // m_instance = new MyCAS; // static CGarhuishou cl; //静态类成员对象 程序退出时调用析构函数 // } //} std::call_once(g_flag, CreateInstance); //两个线程同时执行到这, cout << "执行完成" << endl; //其中一个线程要等另一个指向完毕CreateInstance()。 return m_instance; } class CGarhuishou { //类中套类,用来释放对象 public: ~CGarhuishou() { //类的析构函数中 if (MyCAS::m_instance) { delete MyCAS::m_instance; MyCAS::m_instance = NULL; } } }; void func() { cout << "测试" << endl; }};//类静态变量初始化MyCAS* MyCAS::m_instance = NULL;void mythread() { cout << "开始" << endl; MyCAS *p_a = MyCAS::GetInstance(); //创建一个对象,返回该类对象的指针 cout << "thread执行完成" << endl; return;}//int main()//{// std::thread m1(mythread);// std::thread m2(mythread);// m1.join();// m2.join();//// //cout << "开始" << endl;// ////单例模式不能直接通过classname objname来创建对象,只能通过调用类方法创建类对象。// //MyCAS *p_a = MyCAS::GetInstance(); //创建一个对象,返回该类对象的指针// //MyCAS *p_b = MyCAS::GetInstance(); //再次创建一个对象,返回该类对象的指针其实是一样的// //p_a->func();// //p_b->func();// ////MyCAS::GetInstance()->func(); //先将对象创建并对改装在的数据进行装载,后续只读数据// //cout << "执行完成" << endl;////}
#include<iostream>
#include<mutex>
#include<atomic>
#include<future>
using namespace std;
//原子操作一般指“不可分割的操作”;也就是说着这种操作状态要么是完成的,要么是没完成的,不可能出现半完成状态。
//Std::atomic来代表原子操作,std:atmoic是个类模板,用来封装某个类型的值的。
//原子操作,常用于技术或者统计(累计方出去多少个数据包,累计接收到多少个数据包)
//原子操作一般针对于++/--/+=/-=/&=/|=/^=是支持的,其他的可能不支持,比如:g_mycout=g_mycout+1;
//int g_mycout = 0; //定义一个全局变量
std::atomic<int> g_mycout=0; //封装一个类型为int的对象(值);我们可以像操作一个int类型变量一样老操作这个g_mycout
//std::mutex g_my_mutex; //互斥量
void thread1() {
for (int i = 0; i < 10000000; i++) {
//g_my_mutex.lock(); //实现了20000000次加解锁
g_mycout++; //对应的是一个原子操作(不会 被打断)
//g_my_mutex.unlock();
}
}
std::atomic<bool> g_ifend = false; //线程退出标记,这里是原子操作,防止读和写乱套;
void thread2() {
std::chrono::milliseconds dura(1000);
while (g_ifend == false) {
//系统没要求线程退出,所以本线程可以干自己想干的事情
cout << "this_id" << std::this_thread::get_id() <<"运行中"<< endl;
std::this_thread::sleep_for(dura);
}
cout << "this_id" << std::this_thread::get_id() << "运行中" << endl;
return;
}
int thread3() {
cout << "thread start threadid" << std::this_thread::get_id() << endl;
return 1;
}
//int main()
//{
// /*thread t1(thread1);
// thread t2(thread1);
// t1.join();
// t2.join();
// cout << g_mycout << endl;*/
//
// /*thread t3(thread2);
// thread t4(thread2);
// std::chrono::milliseconds dura(1000);
// std::this_thread::sleep_for(dura);
// g_ifend = true;
// t3.join();
// t4.join();
// cout << "程序执行结束,退出" << endl;
//*/
//
// cout << "main thread_id" << std::this_thread::get_id() << endl;
// std::future<int> result = std::async(thread3);
//
// std::future_status status = result.wait_for(std::chrono::seconds(0)); //result.wait_for(0s)
// if (status == std::future_status::deferred) {
// //线程被延迟执行了(系统资源紧张,它采用了std::launch::deferred策略)
// cout << result.get() << endl;
// }
// else {
// //任务没有被推迟,已经开始运行了,线程被创建了
// if (status == std::future_status::ready) {
// cout << "ready 线程成功执行完毕并返回" <<result.get()<< endl;
// }
// else if (status == std::future_status::timeout) {
// cout << "线程还没执行完 timeout" << endl;
// cout << result.get() << endl; //调用get()等待获取返回结果
// }
// }
// cout << result.get() << endl;
// return 0;
//}