`
阿尔萨斯
  • 浏览: 4151403 次
社区版块
存档分类
最新评论

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 the moment this function returns with the completion of all the operations in the thread: This blocks the execution of the thread that calls this function until the function called on construction returns (if it hasn't yet).

该函数的返回与子线程执行完毕同步,该函数会阻塞调用该函数的线程直到子线程调用完毕。

例子:

#include <iostream>
#include <thread>
#include <vector>
#include <ctime>
using namespace std;
//delay(n) 延时n秒  
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){
	while(n>5){
		cout<<"currentThread is "<<pthread_self()<<",Now n is "<<n<<endl;
		delay(1);
		n--;
	}
}
int main()
{
	cout<<"main starts"<<endl;
	thread t2(show,10);
	//t2.join();
	cout<<"main complete!"<<endl;
}
运行截图:


可以看到,t2还没有执行完毕就已经结束了。

加上t2.join()之后的执行结果:


可以看到,阻塞了主线程,等待t2执行完毕才继续执行main线程。


After a call to this function, thethreadobject becomes non-joinableand can bedestroyedsafely.

调用该函数后,子线程对象变成non-joinable以及可以安全地销毁。


Parameters

none

Return value

none

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
// example for thread::join
#include <iostream>       // std::cout
#include <thread>         // std::thread, std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds
 
void pause_thread(int n) 
{
  std::this_thread::sleep_for (std::chrono::seconds(n));
  std::cout << "pause of " << n << " seconds ended\n";
}
 
int main() 
{
  std::cout << "Spawning 3 threads...\n";
  std::thread t1 (pause_thread,1);
  std::thread t2 (pause_thread,2);
  std::thread t3 (pause_thread,3);
  std::cout << "Done spawning threads. Now waiting for them to join:\n";
  t1.join();
  t2.join();
  t3.join();
  std::cout << "All threads joined!\n";

  return 0;
}


Output (after 3 seconds):
Spawning 3 threads...
Done spawning threads. Now waiting for them to join:
pause of 1 seconds ended
pause of 2 seconds ended
pause of 3 seconds ended
All threads joined!

Data races

The object is modified.
Note that any operations on thethreadobject itself are not synchronized (unlike the operations within the thread it represents).

Exception safety

Basic guarantee:if an exception is thrown by this member function, thethreadobject is left in a valid state.

If the call fails, asystem_errorexception is thrown:
exception typeerror conditiondescription
system_error errc::invalid_argument - Thethreadobject is notjoinable
system_error errc::no_such_process - Thethreadobject is not valid
system_error errc::resource_deadlock_would_occur - The current thread is the same as the thread attempted to join, or
- A deadlock was detected (implementations may detect certain cases of deadlock).

Note that if the thread represented by the object terminates with an uncaught exception, this cannot be caught by the current thread, andterminate()is automatically called.


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

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

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

author:天下无双

Email:coderguang@gmail.com

2014-9-4

于GDUT

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




分享到:
评论

相关推荐

    c++11中关于std::thread的join的详解

    主要介绍了c++11中关于std::thread的join详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    详解C++ thread用法总结

    C++11中加入了&lt;thread&gt;头文件,此头文件主要声明了std::thread线程类。C++11的标准类std::thread对线程进行了封装,定义了C++11标准中的一些表示线程的类、用于互斥访问的类与方法等。应用C++11中的std::thread便于...

    c++11多线程编程

    c++11多线程库的使用,主要介绍了thread类中的构造函数 join函数和detach函数的使用方式

    C++11/14 线程调用类对象和线程传参的方法

    线程调用类对象 在前面的示例中,我们为线程任务使用了通常的函数。实际上,我们可以使用任何可调用对象或者lambda函数,如下调用类对象的例子: #include #include &lt;thread&gt; class MyFunctor ... t.join();

    C++11/14 线程中使用Lambda函数的方法

    多线程中使用lambda 在本篇文章中,主要介绍lambda函数在多线程中的使用。 先从下面的例子开始吧: ...在此基础上我们将创建5个线程,然后把线程放进一个vector容器中, 用for_each()完成线程的汇合(join): #

    c++11新特性多线程操作实战

    c++11多线程操作 线程 thread int main() { thread t1(Test1); t1.join(); thread t2(Test2); t2.join(); thread t3 = t1; thread t4(t1); thread t5 = std::move(t1); thread t6(std::move(t1)); ...

    C++11并发编程实作

    C++11之后加入了并行编程的接口,使用起来非常简单: #include #include void p() { while (true) { std::cout &lt;&lt; asd &lt;&lt; std::endl; } } void main() { std::thread p(p); p.join();//启动多...

    cpp-channel:C++11 的实验性 Go 风格并发

    cpp-通道C++11 的 Go 风格并发入门要使用该库,请#include &lt;channel&gt; 。 下面是一个例子: void thread_a (cpp::channel&lt; char&gt; c) { c. send ( ' A ' ); char r = c. recv (); assert ( ' B ' == r);}void thread_b...

    C++线程的高级封装, 支持对类成员函数开启线程

    Thread t1; t1.Start(ThreadStart(&ThreadFunction, (void*)1)); t1.Join(); Test test; // 类, ThreadMethod为非静态成员 Thread t2; t2.Start(ThreadStart(&test, &Test::ThreadMethod)); t2.Join();

    C++11/14 线程的创建与分离的实现

    线程的创建 让我们看看示例代码(t1.cpp). #include #include &lt;thread&gt; void thread_function() { std::cout &lt;... t.join(); // 主线程等待子线程结束 return 0; } 代码在linux系统下将输出: $ g++ t1.c

    OpenMP+F90并行编程基础

    Based on the fork/join model: the program starts as a single thread at designated parallel regions a pool of threads is formed the threads execute in parallel across the region at the end of the ...

    Brief Introduction to OpenMP

    Based on the fork/join model: the program starts as a single thread at designated parallel regions a pool of threads is formed the threads execute in parallel across the region at the end of the ...

    jthread-lite:单文件标头库中的C ++ 20的C ++ 11和更高版本的jthread

    jthread lite:C ++ 20的jthread(用于C ++ 11和更高版本) 婴儿期的作品。 彼得·费瑟斯通(Peter Featherstone)建议。 内容 用法示例 ... // automatically join thread here, making sure it

    MC504-raytracing

    由于我的主桌面是Windows,因此变得有点复杂,相反,我使用的是C ++ 11中可用的std :: thread等效项。 cversion分支中提供了基于AC的版本。 在这种情况下,pthread_create将替换为: std::thread thread1 ...

    testActiveMQ.rar

    #pragma comment(lib,"Rpcrt4.lib") #endif class HelloWorldProducer : public Runnable { private: Connection* connection; Session* session; Destination* destination; MessageProducer* producer; int ...

    多线程与智能指针.pdf

    C++11线程 #include &lt;thread&gt; void task(int i) { cout &lt;&lt; "task:" ; } thread t1(task,100); //等待线程结束再继续执⾏ t1.join(); POSIX线程 POSIX 可移植操作系统接⼝,标准定义了操作系统应该为应⽤程序提供的...

    深入理解Android:卷I--详细书签版

    6.2.5 秋风扫落叶——StartThread Pool和join Thread Pool分析 149 6.2.6 你彻底明白了吗 152 6.3 服务总管ServiceManager 152 6.3.1 ServiceManager的原理 152 6.3.2 服务的注册 155 6.3.3 ServiceManager...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持Delphi 4-XE5 and C++Builder 6-XE5. D2010以上版本(D14_D19)安装必读 delphi2010以上版本(D14_D19)使用者安装时,请将res\frccD14_...

    Clever Internet Suite (SRC) v9.1.0.0

    The HttpAuthorization is not thread-safe - fixed. SFTP file permissions parsing errors were fixed, IsDir file attribute works correctly. Installer runs with errors on Windows XP - fixed. TLS engine - ...

Global site tag (gtag.js) - Google Analytics