zoukankan      html  css  js  c++  java
  • std::thread

    std::shared_ptr<std::thread> m_spThread;
    
    m_spThread.reset(new std::thread(std::bind(&GameServer::process_thread, this)));
    
    void GameServer::process_thread()
    {
    try
    {
    process_thread_try();
    }
    catch (...)
    {
    DWORD e = GetLastError();
    int a = 0;
    }
    }
    std::bind(&GameServer::process_thread, this)返回一个std::function,绑定成员函数process_thread,然后new std::thread(std::Funciton)返回thread*
    reset源码这样的
    template<class _Ux>
            void reset(_Ux *_Px)
            {    // release, take ownership of _Px
            shared_ptr(_Px).swap(*this);
            }

    就是交换智能指针管理的对象,m_spThread管理这个对象,之后线程函数就自动执行了,我有个疑问为什么这个不用Join或者detach?

    具体看下:

    一个 std::thread 对象可以接收
    
    普通的函数
    函数对象
    类的成员函数
    lambda 函数
    作为参数。
    
    #include <QtCore>
    #include <thread>
    普通的函数
    void test1()
    {
        qDebug()<<"hello test 1";
    }
    
    void test2(const QString &text)
    {
        qDebug()<<"hello"<<text;
    }
    函数对象
    class Test3
    {
    public:
        Test3(){}
        void operator()() const
        {
            qDebug()<<"hello test3"<<this;
        }
    };
    
    class Test4
    {
    public:
        QString a;
        Test4(const QString a):a(a){}
        void operator()(const QString b) const
        {
            qDebug()<<a<<b;
        }
    };
    类的成员函数
    class Test5
    {
    public:
        void output()
        {
            qDebug("hello test 5");
        }
    };
    
    class Test6
    {
    public:
        void output(const QString & text)
        {
            qDebug()<<"hello"<<text;
        }
    };
    以及lambda函数
    主程序
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        std::thread t1(test1);
        std::thread t2(test2, "test 2");
        std::thread t3((Test3()));
        std::thread t4(Test4("hello"), "test 4");
        Test5 test5;
        std::thread t5(&Test5::output, &test5);
        Test6 test6;
        std::thread t6(&Test6::output, &test6, "test 6");
        std::thread t7([](const QString & text){qDebug()<<"hello"<<text;}, "test7");
        return a.exec();
    }
    备忘
    添加了两层括号(不然会被编译器认作一个名为t3的函数的声明)
    
        std::thread t3((Test3()));
    std::bind ? 带参数,比如:
    
        std::thread t2(test2, "test 2");
    要写成
    
        std::thread t2(std::bind(test2, "test 2"));
    那一个标准?
    
    std::ref ? (函数对象,或者参数,都是传值,传引用需要使用std::ref),比如对前面的Test3
    
        Test3 test3;
        test3();
        std::thread t3(test3);
        std::thread t33(std::ref(test3));
    这三次调用的结果(类似于):
    
    hello test3 0xbfc3b27f 
    hello test3 0xbfc3b27f 
    hello test3 0x9811e60 
  • 相关阅读:
    随手练——几个递归小题目
    随手练——USACO 1.44 母亲的牛奶
    随手练——汉诺塔问题(递归典型)
    Linux搭建lamp(Apache+PHP+Mysql环境)centos7.2版详细教程
    Windows 环境下安装redis 及其PHP Redis扩展
    Windows系统如何安装Redis
    phpstorm配置成sublime的代码高亮逼格风格
    phpstorm-----实现实时编辑服务器代码
    如何在Webstorm/Phpstorm中设置连接FTP,并快速进行文件比较,上传下载,同步等操作
    phpstorm取消自动保存,修改快捷键并标识修改的文件为星星标记
  • 原文地址:https://www.cnblogs.com/zzyoucan/p/3622539.html
Copyright © 2011-2022 走看看