学无先后,达者为师

网站首页 编程语言 正文

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