1. // C11multithread.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    2. //
    3. #include <iostream>
    4. #include<thread>
    5. #include<string>
    6. #include<map>
    7. #include<vector>
    8. #include<list>
    9. #include<mutex>
    10. using namespace std;
    11. std::mutex resource_mutex;
    12. std::once_flag g_flag; //这是个系统定义的标记
    13. //单例设计模式
    14. class MyCAS {
    15. static void CreateInstance() { //只被调用一次的函数
    16. std::chrono::milliseconds dura(2000); //休息20秒
    17. std::this_thread::sleep_for(dura);
    18. cout << "call_once()执行完毕" << endl;
    19. m_instance = new MyCAS;
    20. static CGarhuishou cl; //静态类成员对象 程序退出时调用析构函数
    21. }
    22. private:
    23. MyCAS() { //私有化构造对象
    24. }
    25. private:
    26. static MyCAS *m_instance; //静态成员变量
    27. public:
    28. static MyCAS *GetInstance() {
    29. //提高效率:
    30. //如果if(m_instance!=NULL)条件成立,则肯定表示m_instance已经被new过了
    31. //如果if(m_instance==NULL),不代表m_instance一定被new过了。
    32. //if (m_instance == NULL) { //双重锁定(双重检查)
    33. // std::unique_lock<std::mutex> myobj(resource_mutex);
    34. // if (m_instance == NULL) {
    35. // m_instance = new MyCAS;
    36. // static CGarhuishou cl; //静态类成员对象 程序退出时调用析构函数
    37. // }
    38. //}
    39. std::call_once(g_flag, CreateInstance); //两个线程同时执行到这,
    40. cout << "执行完成" << endl; //其中一个线程要等另一个指向完毕CreateInstance()。
    41. return m_instance;
    42. }
    43. class CGarhuishou { //类中套类,用来释放对象
    44. public:
    45. ~CGarhuishou() { //类的析构函数中
    46. if (MyCAS::m_instance) {
    47. delete MyCAS::m_instance;
    48. MyCAS::m_instance = NULL;
    49. }
    50. }
    51. };
    52. void func() {
    53. cout << "测试" << endl;
    54. }
    55. };
    56. //类静态变量初始化
    57. MyCAS* MyCAS::m_instance = NULL;
    58. void mythread() {
    59. cout << "开始" << endl;
    60. MyCAS *p_a = MyCAS::GetInstance(); //创建一个对象,返回该类对象的指针
    61. cout << "thread执行完成" << endl;
    62. return;
    63. }
    64. //int main()
    65. //{
    66. // std::thread m1(mythread);
    67. // std::thread m2(mythread);
    68. // m1.join();
    69. // m2.join();
    70. //
    71. // //cout << "开始" << endl;
    72. // ////单例模式不能直接通过classname objname来创建对象,只能通过调用类方法创建类对象。
    73. // //MyCAS *p_a = MyCAS::GetInstance(); //创建一个对象,返回该类对象的指针
    74. // //MyCAS *p_b = MyCAS::GetInstance(); //再次创建一个对象,返回该类对象的指针其实是一样的
    75. // //p_a->func();
    76. // //p_b->func();
    77. // ////MyCAS::GetInstance()->func(); //先将对象创建并对改装在的数据进行装载,后续只读数据
    78. // //cout << "执行完成" << endl;
    79. //
    80. //}
    #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;
    //}