C++(二)多线程

 

c++多线程学习,包含头文件、异步、睡眠、线程交换、线程移动和线程传参。

1. 线程头文件

C++11 新标准中引入了五个头文件来支持多线程编程

<atomic>:该头文主要声明了两个类, std::atomic 和 std::atomic_flag,另外还声明了一套 C 风格的原子类型和与 C 兼容的原子操作的函数。 <thread>:该头文件主要声明了 std::thread 类,另外 std::this_thread 命名空间也在该头文件中。 <mutex>:该头文件主要声明了与互斥量(mutex)相关的类,包括 std::mutex 系列类,std::lock_guard, std::unique_lock, 以及其他的类型和函数。 <condition_variable>:该头文件主要声明了与条件变量相关的类,包括 std::condition_variable 和 std::condition_variable_any。 <future>:该头文件主要声明了 std::promise, std::package_task 两个 Provider 类,以及 std::future 和 std::shared_future 两个 Future 类,另外还有一些与之相关的类型和函数,std::async() 函数就声明在此头文件中。

2. 线程异步

#include<thread>
using namespace std;
void show()
{
    cout << "hello cplusplus!" << endl;
}
int main()
{
    //栈上
    thread t1(show);   //根据函数初始化执行
    thread t2(show);
    thread t3(show);
    //线程数组
    thread th[3]{thread(show), thread(show), thread(show)};
    //堆上
    thread *pt1(new thread(show));
    thread *pt2(new thread(show));
    thread *pt3(new thread(show));
    //线程指针数组
    thread *pth(new thread[3]{thread(show), thread(show), thread(show)});
    return 0;
}

3. 线程睡眠

#include<thread>
#include<chrono>
using namespace std;
int main()
{
    thread th1([]()
    {
        //让线程等待3秒
        this_thread::sleep_for(chrono::seconds(3));
        //让cpu执行其他空闲的线程
        this_thread::yield();
        //线程id
        cout << this_thread::get_id() << endl;
    });
    return 0;
}

4. 线程交换

#include<thread>
using namespace std;
int main()
{
    thread t1([]()
    {
        cout << "thread1" << endl;
    });
    thread t2([]()
    {
        cout << "thread2" << endl;
    });
    cout << "thread1' id is " << t1.get_id() << endl;
    cout << "thread2' id is " << t2.get_id() << endl;

    cout << "swap after:" << endl;
    swap(t1, t2);//线程交换
    cout << "thread1' id is " << t1.get_id() << endl;
    cout << "thread2' id is " << t2.get_id() << endl;
    return 0;
}
运行结果:
thread1
thread2
thread1' id is 4836
thread2' id is 4724
swap after:
thread1' id is 4724
thread2' id is 4836

5. 线程移动

#include<thread>
using namespace std;
int main()
{
    thread t1([]()
    {
        cout << "thread1" << endl;
    });
    cout << "thread1' id is " << t1.get_id() << endl;
    thread t2 = move(t1);;
    cout << "thread2' id is " << t2.get_id() << endl;
    return 0;
}
运行结果:
thread1
thread1' id is 5620
thread2' id is 5620

6. 线程传参

#include<thread>
#include<chrono>
using namespace std;
void fun1(int n)  //初始化构造函数
{
    cout << "Thread " << n << " executing\n";
    n += 10;
    this_thread::sleep_for(chrono::milliseconds(10));
}
void fun2(int & n) //拷贝构造函数
{
    cout << "Thread " << n << " executing\n";
    n += 20;
    this_thread::sleep_for(chrono::milliseconds(10));
}
int main()
{
    int n = 0;
    thread t1;               //t1不是一个thread
    thread t2(fun1, n + 1);  //按照值传递
    t2.join();
    cout << "n=" << n << '\n';
    n = 10;
    thread t3(fun2, ref(n)); //引用
    thread t4(move(t3));     //t4执行t3,t3不是thread
    t4.join();
    cout << "n=" << n << '\n';
    return 0;
}
运行结果:
Thread 1 executing
n=0
Thread 10 executing
n=30