C++的线程管理-线程类(Thread)

执行线程是一个指令序列,它可以在多线程环境中,与其他此类指令序列同时执行,同时共享相同的地址空间。

一个初始化的线程对象代表一个活动的执行线程;这样的线程对象是可连接的,并且具有唯一的线程ID。

默认构造的(未初始化的)线程对象是不可连接的,并且它的线程 id 对于所有不可连接的线程都是通用的。

如果从可连接线程移出,或者对它们调用 join 或 detach,则该线程将变得不可连接。

#include <iostream>
#include <thread>
#include <unistd.h>

using namespace std;

void foo() 
{
  sleep(10);  //  sleep 10 seconds
  cout << "I'm foo, awake now" << endl;
}

void bar(int x)
{
  sleep(5);  //  sleep 10 seconds
  cout << "I'm bar, awake now" << endl;
}

int main() 
{
  thread T1 (foo);     // spawn new thread that calls foo()
  thread T2 (bar,0);  // spawn new thread that calls bar(0)

  cout << "main, foo and bar now execute concurrently...\n";

  // synchronize threads:
  T1.join();                // pauses until first finishes
  T2.join();               // pauses until second finishes

  cout << "foo and bar completed.\n";

  return 0;
}

程序运行屏幕输出

main, foo and bar now execute concurrently...
I'm bar, awake now
I'm foo, awake now
foo and bar completed.

线程构造器


 - thread() noexcept;
 - template <class Fn, class... Args>explicit thread (Fn&& fn, Args&&... args); 
 - thread (const thread&) = delete; 
 - thread (thread&& x) noexcept;

约定构造器

thread() noexcept;

构造一个线程对象, 它不包含任何执行线程。

初始化构造器

template <class Fn, class... Args>explicit thread (Fn&& fn, Args&&... args);

构造一个线程对象,它拥有一个可连接执行线程。
新的执行线程调用 fn, 并传递 args 作为参数。
此构造的完成与 fn 的副本开始运行同步。

#include <chrono>
#include <iostream>
#include <thread>
#include <utility>

using namespace std;

void f1(int n)
{
    for (int i = 0; i < 5; ++i)
    {
        cout << "Thread 1 executing\n";
        ++n;
        this_thread::sleep_for(chrono::milliseconds(10));
    }
}
 
void f2(int& n, int sz)
{
    for (int i = 0; i < sz; ++i)
    {
        cout << "Thread 2 executing\n";
        ++n;
        this_thread::sleep_for(chrono::milliseconds(10));
    }
}
 
int main()
{
    int n = 0;

    thread t2(f1, n + 1); 		 // pass by value
    thread t3(f2, ref(n), 6); // pass by reference

    t2.join();
	 t3.join();
	 
    cout << "Final value of n is " << n << '\n';
}

程序运行屏幕输出

Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 2 executing
Thread 1 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 2 executing
Final value of n is 6

复制构造器

thread (const thread&) = delete; 

删除构造函数,线程对象不能复制。

移动构造器

thread (thread&& x) noexcept;

构造一个线程对象,该对象获取 x 表示的执行线程(如果有)。此操作不会以任何方式影响移动线程的执行,它只是传输其处理程序。
x 对象不再代表任何执行线程。

#include <chrono>
#include <iostream>
#include <thread>
#include <utility>
#include <unistd.h>

using namespace std;

void f2(int& n)
{
	thread::id this_id = this_thread::get_id();
   cout << "Thread " << this_id << " executing" << endl;
	 
    for (int i = 0; i < 5; ++i)
    {
        ++n;
        this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}
 
int main()
{
    int n = 0;
	 
    thread t3(f2, ref(n));
    thread t4(move(t3));

    t4.join();

    cout << "Final value of n is " << n << '\n';
}

程序运行屏幕输出

Thread 140291256411904 executing
Final value of n is 5
上一篇:浅谈化工厂环保管理的痛点、智慧环保的必要性及EHS系统的实现路径


下一篇:Apache配置与应用(优化apache)