• std::thread


    td::thread 各种构造函数例子如下:

    #include <iostream>
    #include <utility>
    #include <thread>
    #include <chrono>
    #include <functional>
    #include <atomic>
    
    void f1(int n)
    {
        for (int i = 0; i < 5; ++i) {
            std::cout << "Thread " << n << " executing
    ";
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
    
    void f2(int& n)
    {
        for (int i = 0; i < 5; ++i) {
            std::cout << "Thread 2 executing
    ";
            ++n;
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
    
    int main()
    {
        int n = 0;
        std::thread t1; // t1 is not a thread
        std::thread t2(f1, n + 1); // pass by value
        std::thread t3(f2, std::ref(n)); // pass by reference
        std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
        t2.join();
        t4.join();
        std::cout << "Final value of n is " << n << '
    ';
    }
    root@ubuntu:~/c++#  g++ -std=c++11 -pthread obj.cpp  -o obj
    root@ubuntu:~/c++# ./obj 
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Final value of n is 5
    root@ubuntu:~/c++# 

     

    int main()
    {
        int n = 0;
        std::thread t1; // t1 is not a thread
        std::thread t2(f1, n + 1); // pass by value
        std::thread t3(f2, std::ref(n)); // pass by reference
        t2.join();
        t3.join();
        std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
        t4.join();
        std::cout << "Final value of n is " << n << '
    ';
    }
    root@ubuntu:~/c++#  g++ -std=c++11 -pthread obj.cpp  -o obj
    root@ubuntu:~/c++# ./obj 
    Thread Thread 2 executing
    1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    terminate called after throwing an instance of 'std::system_error'
      what():  Invalid argument
    Aborted
    root@ubuntu:~/c++# 
    int main()
    {
        int n = 0;
        std::thread t1; // t1 is not a thread
        std::thread t2(f1, n + 1); // pass by value
        std::thread t3(f2, std::ref(n)); // pass by reference
        t2.join();
        t3.join();
        std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
        //t4.join();
        std::cout << "Final value of n is " << n << '
    ';
    }
    root@ubuntu:~/c++#  g++ -std=c++11 -pthread obj.cpp  -o obj
    root@ubuntu:~/c++# ./obj 
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Final value of n is 5
    root@ubuntu:~/c++# 

    std::thread 赋值操作

    Move 赋值操作 thread& operator=(thread&& rhs) noexcept;
    拷贝赋值操作 [deleted] thread& operator=(const thread&) = delete;
    • Move 赋值操作(1),如果当前对象不可 joinable,需要传递一个右值引用(rhs)给 move 赋值操作;如果当前对象可被 joinable,则会调用 terminate() 报错。
    • 拷贝赋值操作(2),被禁用,因此 std::thread 对象不可拷贝赋值。
    #include <iostream>
    #include <utility>
    #include <thread>
    #include <chrono>
    #include <functional>
    #include <atomic>
    #include <vector>
    std::vector<std::thread> vthreads;
    int n=0;
    void f1(int n)
    {
        for (int i = 0; i < 5; ++i) {
            std::cout << "Thread " << n << " executing
    ";
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
    
    void f2(int& n)
    {
        for (int i = 0; i < 5; ++i) {
            std::cout << "Thread 2 executing
    ";
            ++n;
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
    
    int create_thread()
    {
        std::thread t2(f1, n + 1); // pass by value
        std::thread t3(f2, std::ref(n)); // pass by reference
        vthreads.push_back(std::move(t2));
        vthreads.push_back(std::move(t3));
    }
    int main()
    {
        create_thread();
       // for(auto it = vthreads.begin(); it != vthreads.end(); ++it)
       // {
       //                 it->join();
       // }
    
       for ( auto & th :vthreads)
       {
               th.join();
       }
       std::cout << "Final value of n is " << n << '
    ';
       return 0;
    }
    root@ubuntu:~/c++#  g++ -std=c++11 -pthread obj.cpp  -o obj
    root@ubuntu:~/c++# ./obj 
    Thread Thread 2 executing
    1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Thread 2 executing
    Thread 1 executing
    Final value of n is 5
  • 相关阅读:
    Map接口框架图
    Collection接口框架图
    Java集合框架源码(四)——Vector
    Java集合框架源码(三)——arrayList
    HashSet与HashMap的区别
    Java集合框架源码(二)——hashSet
    hashMap与hashTable的区别
    HashMap与ConcurrentHashMap的区别
    asp.net 项目Net4.0 在IE10、 IE 11 下出现 “__doPostBack”未定义 的解决办法
    C# 完整List例子
  • 原文地址:https://www.cnblogs.com/dream397/p/14597026.html
Copyright © 2020-2023  润新知