美文网首页
一个关于线程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的例子

    #include "pch.h" #include #include using namespace std; v...

  • pthread-detach()

    pthread_detach()函数: pthread_detach,是计算机用语,创建一个线程默认的状态是jo...

  • C多线程 队列

    join,detach thread::join(): 阻塞当前线程,直至 this 所标识的线程完成其执行。th...

  • posix-pthread (1)

    进程和线程api对比 知识点 1 使用pthread_detach 方法脱离一个线程就不会产生僵线程。2 获取当...

  • 关于detach

    将variable参数从网络中隔离开。保持一部分的网络参数不变。 我后来的疑问是,他会在optimizer那里被更...

  • C++11多线程-【2】线程的join和detach

    C++11多线程-【2】线程的join和detach 本文翻译自 C++11 Multithreading – P...

  • Pthread

    线程的join和detach两个状态 当一个可汇合的线程终止时,它的线程ID和退出状态将留到另一个线程对它调用pt...

  • 3. Thread.h——封装thread

    该类封装了thread的create、join、detach等操作。 多线程中系统中将要大量使用线程操作函数。为了...

  • threads_join,pthread_detach

    pthread_join与pthread_detach pthread_join函数会让主线程阻塞,直到所有线程都...

  • C++ concurrency in action: 1~4 章

    1 概述 2 管理线程: thread/join/detach/RAAI/std::ref/std::bind/m...

网友评论

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

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