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, the thread object becomes non-joinable and
can be destroyed safely.

调用该函数后,子线程对象变成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;
}

Edit
& Run

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 the thread object 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, the thread object is
left in a valid state.

If the call fails, a system_error exception is thrown:

exception type error condition description
system_error errc::invalid_argument - The thread object is not joinable
system_error errc::no_such_process - The thread object 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, and terminate() is
automatically called.

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

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

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

author:天下无双

Email:[email protected]

2014-9-4

于GDUT

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

时间: 2024-08-29 03:27:24

C++11 thread::join(4)的相关文章

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

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

thread.join函数,java多线程中的join函数解析

join函数的作用,是让当前线程等待,直到调用join()的 线程结束或者等到一段时间,我们来看以下代码 1 package mian; 2 3 4 public class simpleplela { 5 static void threadMessage(String message) { 6 String threadName = 7 Thread.currentThread().getName(); 8 9 System.out.println(threadName+" "+m

初探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

java多线程同步以及线程间通信详解&amp;消费者生产者模式&amp;死锁&amp;Thread.join()(多线程编程之二)

本篇我们将讨论以下知识点: 1.线程同步问题的产生 什么是线程同步问题,我们先来看一段卖票系统的代码,然后再分析这个问题: [java] view plain copy print? package com.zejian.test; /** * @author zejian * @time 2016年3月12日 下午2:55:42 * @decrition 模拟卖票线程 */ public class Ticket implements Runnable { //当前拥有的票数 private 

浅析 Java Thread.join()

一.在研究join的用法之前,先明确两件事情. 1.join方法定义在Thread类中,则调用者必须是一个线程, 例如: Thread t = new CustomThread();//这里一般是自定义的线程类 t.start();//线程起动 t.join();//此处会抛出InterruptedException异常 2.上面的两行代码也是在一个线程里面执行的. 以上出现了两个线程,一个是我们自定义的线程类,我们实现了run方法,做一些我们需要的工作:另外一个线程,生成我们自定义线程类的对象

java中synchronize锁 volatile thread.join()方法的使用

对于并发工作,你永远不知道一个线程何时运行,你需要某种方式来避免两个任务访问相同的资源,即要避免资源竞争,至少在关键代码上不能出现这样的情况,否则多个线程同时对某个内存区域操作会导致数据破坏. 程序代码中的临界区是需要互斥访问的,同一时刻只能有一个线程来访问临界区,也就是线程对临界区的访问时互斥的. 竞争条件:当多个线程同时访问某个共享的内存区域并且对其进行读写操作时,就会出现数据破坏.这就是竞争条件.避免竞争条件的方法是synchronized加锁. 样例,设有一个现成,该线程的任务是对共享变

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

thread.Join(); 让主线程等待自己完成

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication1 { class Program { private static void Method() { Thread.Sleep(5000); Console.WriteLine("当前线程:" + Thread.C

Thread.join()的使用

代码清单: package com.baidu.nuomi.concurrent; import java.util.concurrent.TimeUnit; /** * Created by sonofelice on 16/6/18. */ public class Join { public static void main(String[] args) throws Exception{ Thread previous = Thread.currentThread(); for (int