日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++ std::thread 線程的傳參方式

作者:TheOldManAndTheSea 更新時間: 2022-05-13 編程語言

C++ std::thread 線程的傳參方式

flyfish

標準 C++11

傳遞一個函數的方式

#include 
#include 

void thread_function()
{
    std::cout << "thread function\n";
}

int main()
{
    std::thread t(&thread_function);   // t starts running
    std::cout << "main thread\n";
    t.join();   // 主線程等待線程t完成
    std::cout << "end\n";
    return 0;
}

柏拉圖理念世界的樣子

在這里插入圖片描述

柏拉圖可感世界的樣子

在這里插入圖片描述
結果

main thread
thread function
end

傳值的方式

#include 
#include 
#include 

void thread_function(std::string s)
{
    std::cout << "t thread is = " << s << std::endl;
}

int main()
{
    std::string s = "abc";
    std::thread t(&thread_function, s);
    std::cout << "main thread message = " << s << std::endl;
    t.join();
    return 0;
}
main thread message = abc
t thread is = abc

傳址的方式

#include 
#include 
#include 


void thread_function(std::string &s)
{
    std::cout << "t thread is = " << s << std::endl;
    s = "cde";
}
int main()
{
    std::string s = "abc";
    std::thread t(&thread_function, std::ref(s));
    t.join();
    std::cout << "main thread message = " << s << std::endl;

    return 0;
}

結果

t thread is = abc
main thread message = cde

在線程之間不共享內存(不復制)的方式

#include 
#include 
#include 


void thread_function(std::string s)
{
    std::cout << "t thread is = " << s << std::endl;
    s = "cde";
}
int main()
{
    std::string s = "abc";
    std::thread t(&thread_function, std::move(s));
    t.join();
    std::cout << "main thread message = " << s << std::endl;

    return 0;
}

結果

t thread is = abc
main thread message = 

函數對象的方式

#include 
#include 

class thread_obj {
public:
    void operator()(int x)
    {
        for (int i = 0; i < x; i++)
            std::cout << "Thread using function object as callable\n";
    }
};

int main()
{
    std::thread th(thread_obj(), 3);
    th.join();
    return 0;
}

Lambda 表達式的方式

#include 
#include 

int main()
{

    // Define a Lambda Expression
    auto f = [](int x) {
        for (int i = 0; i < x; i++)
            std::cout << "Thread using lambda expression as callable\n";
    };

    std::thread th(f, 3);
    th.join();

    return 0;
}

傳遞類的成員函數的方式

#include 
#include 

class SayHello
{
public:
    void greeting(std::string const& message) const
    {
        std::cout<

堆分配對象的方式

#include 
#include 
class SayHello
{
public:
    void greeting(std::string const& message) const
    {
        std::cout<<message<<std::endl;
    }
};

int main()
{
    std::shared_ptr<SayHello> p(new SayHello);
    std::thread t(&SayHello::greeting,p,"hello");
    t.join();
}

線程可移動構造 (MoveConstructible) 或移動賦值 (MoveAssignable) ;

不可復制構造 (CopyConstructible) 或復制賦值 (CopyAssignable)

#include 
#include 

void thread_function()
{
    std::cout << "t thread function\n";
}

int main()
{
    std::thread t(&thread_function);
    std::cout << "main thread\n";
    std::thread t2 = std::move(t);

    t2.join();

    return 0;
}

結果

main thread
t thread function

原文鏈接:https://blog.csdn.net/flyfish1986/article/details/123535966