美文网首页
一个关于线程detach的例子

一个关于线程detach的例子

作者: 风之谷rr | 来源:发表于2020-08-30 09:37 被阅读0次

    #include "pch.h"

    #include <iostream>

    #include <thread>

    using namespace std;

    void myPrint(const int& i, char * buf)

    {

    //++i;

    cout << i << endl;

    cout << buf << endl;

    return;

    }

    int main()

    {

    //int v = 1;

    //int &va = v;

    //const char *buf = "this is a test!";

    ////myPrint(v, buf);

    //thread mytobj(myPrint, v, buf);

    ////mytobj.join();

    //mytobj.detach();

    //  std::cout << "Hello World!\n" << v;

    int v = 1;

    int &va = v;

    char buf[] = "this is a test!";

    //myPrint(v, buf);

    thread mytobj(myPrint, v, buf);

    //mytobj.join();

    mytobj.detach();

    std::cout << "Hello World!\n" << v;

    }

    #include "pch.h"

    #include <iostream>

    #include <thread>

    using namespace std;

    class MyClass

    {

    public:

    int i;

    MyClass(int i) :i(i)

    {

    cout << "construct function exec " << endl;

    }

    MyClass(const MyClass &a) :i(a.i) { cout << "copy construct function exec " << endl; }

    ~MyClass()

    {

    cout << "disconstruct function exec " << endl;

    }

    private:

    };

    void myPrint(const int& i, const MyClass buf)

    {

    //++i;

    cout << i << endl;

    cout << &buf << endl;

    return;

    }

    int main()

    {

    //int v = 1;

    //int &va = v;

    //const char *buf = "this is a test!";

    ////myPrint(v, buf);

    //thread mytobj(myPrint, v, buf);

    ////mytobj.join();

    //mytobj.detach();

    //  std::cout << "Hello World!\n" << v;

    int v = 1;

    int &va = v;

    char buf[] = "this is a test!";

    //myPrint(v, buf);

    thread mytobj(myPrint, v, MyClass(v));

    //mytobj.join();

    mytobj.detach();

    //std::cout << "Hello World!\n" << v;

    }

    相关文章

      网友评论

          本文标题:一个关于线程detach的例子

          本文链接:https://www.haomeiwen.com/subject/whgssktx.html