学习使用thread构造函数
从thread定义中(有些地方看不太懂,错了还望指正)
1 无参构造
thread() _NOEXCEPT
{ // construct with no thread
_Thr_set_null(_Thr);
}
没有参数的构造thread() noexceppt 创建空thread对象
-----------------------------------------------------------------------------
2右值引用参数
thread(thread&& _Other) _NOEXCEPT
: _Thr(_Other._Thr)
{ // move from _Other
_Thr_set_null(_Other._Thr);
}
支持 右值引用参数 复制
-----------------------------------------------------------------------------
3普通引用拷贝构造(不支持)
thread(const thread&) = delete;
无法使用
-----------------------------------------------------------------------------
4普通初始化构造函数
template<class _Fn,
class... _Args,
class = typename enable_if<
!is_same<typename decay<_Fn>::type, thread>::value>::type>
explicit thread(_Fn&& _Fx, _Args&&... _Ax)
{ // construct with _Fx(_Ax...)
_Launch(&_Thr,
_STD make_unique<tuple<decay_t<_Fn>, decay_t<_Args>...> >(
_STD forward<_Fn>(_Fx), _STD forward<_Args>(_Ax)...));
}
改构造函数创建线程,调用执行fx函数,args为fx参数
a thread example 改自:http://en.cppreference.com/w/cpp/thread/thread/thread
#include<iostream>
#include<utility>
#include<thread>
#include<chrono>
#include<functional>
#include<atomic>
#include<cstdio>
void f1(int n)
{
for (int i = 0; i < 5; ++i)
{
std::cout << "Threadf1 " <<n << "executing\n";
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void f2(int &n)
{
for (int i = 0; i < 5; ++i)
{
std::cout << "Thread f2 executing\n";
++n;
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
}
int main()
{
int n = 0;
std::thread t1; //t1 is not a thread
std::thread t2(f1, n + 1); //传值
std::thread t3(f2, std::ref(n)); //传引用
std::thread t4(std::move(t3)); //可以右值应用
//std::thread t5(t2); //不可被引用
//t3.join(); // t4右值引用过 不可再被使用
t2.join();
t4.join();
std::cout << "Final value of n is " << n << '\n';
}
另外=赋值
1支持右值赋值(上边拷贝也是)
thread& operator=(thread&& _Other) _NOEXCEPT
{ // move from _Other
return (_Move_thread(_Other));
}
2不支持普通的操作赋值(上边拷贝也是)
thread& operator=(const thread&) = delete;
关于赋值的例子
#include <stdio.h>
#include <stdlib.h>
#include <chrono> // std::chrono::seconds
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
void thread_task(int n) {
std::this_thread::sleep_for(std::chrono::seconds(n));
std::cout << "hello thread "
<< std::this_thread::get_id()
<< " paused " << n << " seconds" << std::endl;
}
/*
* === FUNCTION =========================================================
* Name: main
* Description: program entry routine.
* ========================================================================
*/
int main(int argc, const char *argv[])
{
std::thread threads[5];
std::cout << "Spawning 5 threads...\n";
for (int i = 0; i < 5; i++) {
threads[i] = std::thread(thread_task, i + 1); //这里就是右值赋值thread& operator=(thread&& _Other) _NOEXCEPT
}
/*threads[2] = threads[0];*/ //这样是不对的
std::cout << "Done spawning threads! Now wait for them to join\n";
for (auto& t : threads) {
t.join();
}
std::cout << "All threads joined.\n";
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */