C++11 thread::joinable(5)

原文地址:http://www.cplusplus.com/reference/thread/thread/joinable/

public member function

<thread>

std::thread::joinable

bool joinable() const noexcept;

Check if joinable

Returns whether the thread object is joinable.

返回线程对象是否是joinable的。

thread object is joinable if it represents a thread of execution.

如果是一个正在执行的线程,那么它是joinable的。

thread object is not joinable in any of these cases:

下列任一情况都是非joinable

例子:

#include <iostream>
#include <thread>
#include <ctime>
using namespace std;
void delay(double sec)
{
    time_t start_time, cur_time; // 变量声明
    time(&start_time);
    do {
        time(&cur_time);
        }while((cur_time - start_time) < sec );
}; 

void show(int n){
	cout<<"n="<<n<<endl;
}
thread returnThread(){
	thread tt(show,10);
	return tt;
}

int main()
{

	thread t(show,18);
	cout<<"t is joinable? "<<t.joinable()<<endl;

	thread t1(returnThread());
	cout<<"t1 is joinable? "<<t1.joinable()<<endl;

	thread t2(show,3);
	cout<<"t2 is joinable? "<<t2.joinable()<<endl;
	t2.join();
	cout<<"after t2.join(),t2 is joinable? "<<t2.joinable()<<endl;

	thread t3(show,5);
	cout<<"t3 is joinable? "<<t3.joinable()<<endl;
	t3.detach();
	cout<<"after t3.detach(),t3 is joinable? "<<t3.joinable()<<endl;

}

运行结果:

用GDB调试发现一个好神奇的东西。

运行完了

	cout<<"after t3.detach(),t3 is joinable? "<<t3.joinable()<<endl;

之后,居然像栈析解一样,好神奇阿,现在还不知道为什么呢。。

先保留着,下次解决。

Parameters

none

Return value

true if the thread is joinable.

false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// example for thread::joinable
#include <iostream>       // std::cout
#include <thread>         // std::thread

void mythread()
{
  // do stuff...
}

int main()
{
  std::thread foo;
  std::thread bar(mythread);

  std::cout << "Joinable after construction:\n" << std::boolalpha;
  std::cout << "foo: " << foo.joinable() << ‘\n‘;
  std::cout << "bar: " << bar.joinable() << ‘\n‘;

  if (foo.joinable()) foo.join();
  if (bar.joinable()) bar.join();

  std::cout << "Joinable after joining:\n" << std::boolalpha;
  std::cout << "foo: " << foo.joinable() << ‘\n‘;
  std::cout << "bar: " << bar.joinable() << ‘\n‘;

  return 0;
}

Edit
& Run

Output (after 3 seconds):

Joinable after construction:
foo: false
bar: true
Joinable after joining:
foo: false
bar: false

Data races

The object is accessed.

Exception safety

No-throw guarantee: never throws exceptions.

—————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:[email protected]

2014-9-4

于GDUT

——————————————————————————————————————————————————————————————————

时间: 2024-12-19 12:49:01

C++11 thread::joinable(5)的相关文章

再探c++11 Thread库之原子操作

我在之前一篇博文<初探c++11 Thread库之使写多线程程序>中,着重介绍了<thread>头文件中的std::thread类以及其上的一些基本操作,至此我们动手写多线程程序已经基本没有问题了.但是,单线程的那些"坑"我们仍还不知道怎么去避免. 多线程存在的问题 多线程最主要的问题就是共享数据带来的问题.如果共享数据都是只读的,那么没问题,因为只读操作不会影响到数据,更不会涉及对数据的修改,所以所有线程都会获得同样的数据.但是,当一个或多个线程要修改共享数据

初探C++11 Thread

Thread 开启一个线程 使用c++11开启一个线程是比较简单的,如下: #include<iostream> #include<thread> using namespace std; void hello() { cout<<"hello kitty"<<endl; } int main() { std::thread t(hello); t.join(); return 0; } 输出结果: 也可以通过函数对象的方式 #inclu

C++ 11 thread 基础用法 lock unlock join mutex joinable lock_guard unique_lock condition_variable wait notify_one notify_all asnyc future packaged_task promise

#include "pch.h"#include<iostream> #include<string> #include<vector> #include<list> // 线程相关头文件#include<thread>#include<mutex> #include<future>using namespace std; static int res = 0; //共享变量 演示使用互斥量读写. mu

C++11 thread(1)

原文地址:http://www.cplusplus.com/reference/thread/thread/thread/ public member function <thread> std::thread::thread default (1) thread() noexcept; initialization (2) template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&

C++11 thread::detach(2)

原文地址:http://www.cplusplus.com/reference/thread/thread/detach/ public member function <thread> std::thread::detach void detach(); Detach thread Detaches the thread represented by the object from the calling thread, allowing them to execute independen

C++11 thread::join(4)

原文地址:http://www.cplusplus.com/reference/thread/thread/join/ public member function <thread> std::thread::join void join(); Join thread The function returns when the thread execution has completed. 当该线程执行完成后才返回.(即等待子线程执行完毕才继续执行主线程) This synchronizes

c++11 Thread库初探

c++11中最重要的特性之一就是对多线程的支持了,然而<c++ primer>5th却没有这部分内容的介绍,着实人有点遗憾.在网上了解到了一些关于thread库的内容.这是几个比较不错的学习thread库的资源: Thread support library                    : http://en.cppreference.com/w/cpp/thread Cpp reference/thread                      : http://www.cpl

C++11 thread

1 //这里使用c++的thread创建了5个线程,并支持传递多个参数 2 void thread1(int aa,int bb) 3 { 4 cout << aa << bb << endl; 5 } 6 7 void testthread() 8 { 9 int ithreads[] = {1,2,3,4,5}; 10 for(auto i: ithreads) 11 { 12 thread t1(thread1,i,i+1); 13 t1.join(); 14 }

c++11: &lt;thread&gt;学习

<thread>头文件中包含thread类与this_thread命名空间,下面逐一介绍. thread类 1. 构造函数 (1)默认构造函数 thread() noexcept; 默认构造函数不执行任何线程,产生线程对象的线程ID为0. (2)初始化构造函数 template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&... args); 产生一个thread对象,提供一个j