C ++ 11件のスレッドスワップ

スワップ 

交換2つのスレッド・オブジェクトの基本的なハンドル。

Exchangeオブジェクトへのハンドル二つのスレッドを根底。

// threadTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <string>
#include <chrono>
#include <mutex>
using namespace std;

void foo()
{
	std::this_thread::sleep_for(std::chrono::seconds(1));
}

void bar()
{
	std::this_thread::sleep_for(std::chrono::seconds(1));
}

int main()
{
	std::thread t1(foo);
	std::thread t2(bar);

	std::cout << "thread 1 id: " << t1.get_id() << std::endl;
	std::cout << "thread 2 id: " << t2.get_id() << std::endl;

	t1.swap(t2);
	std::cout << "after std::swap(t1,t2)" << std::endl;
	std::cout << "thread 1 id: " << t1.get_id() << std::endl;
	std::cout << "thread 2 id: " << t2.get_id() << std::endl;

	t1.swap(t2);
	std::cout << "after std::swap(t1,t2)" << std::endl;
	std::cout << "thread 1 id: " << t1.get_id() << std::endl;
	std::cout << "thread 2 id: " << t2.get_id() << std::endl;

	t1.join();
	t2.join();
    return 0;
}

結果:

公開された257元の記事 ウォン称賛22 ビュー90000 +

おすすめ

転載: blog.csdn.net/qq_24127015/article/details/104797885