C++使用Lambda函数实现多线程

#include 
#include 
#include 
#include 

int main()
{
    // vector 容器存储线程
    std::vector workers;
    for (int i = 0; i < 5; i++) 
    {
        workers.push_back(std::thread([]() 
        {
            std::cout << "thread function\n";
        }));
    }
    std::cout << "main thread\n";

    // 通过 for_each 循环每一个线程
    // 第三个参数赋值一个task任务
    // 符号'[]'会告诉编译器我们正在用一个匿名函数
    // lambda函数将它的参数作为线程的引用t
    // 然后一个一个的join
    std::for_each(workers.begin(), workers.end(), [](std::thread &t;) 
    {
        t.join();
    });

    return 0;
}
#include 
#include 
#include 

int main()
{
    std::string a("Hello");
    std::cout << "address = " << &a << "\n";
    std::thread t([](std::string& a)
        {
            std::cout << "in thread address = " << &a << "\n";
        }, a);
    t.join();
}
In file included from /usr/include/c++/4.7/bits/move.h:57:0,
                 from /usr/include/c++/4.7/bits/stl_pair.h:61,
                 from /usr/include/c++/4.7/bits/stl_algobase.h:65,
                 from /usr/include/c++/4.7/bits/char_traits.h:41,
                 from /usr/include/c++/4.7/ios:41,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from test.cpp:1:
/usr/include/c++/4.7/type_traits: In instantiation of ?.truct std::_Result_of_impl, std::basic_string, std::allocator > >?.
/usr/include/c++/4.7/type_traits:1857:12:   required from ?.lass std::result_of(std::basic_string)>?
/usr/include/c++/4.7/functional:1563:61:   required from ?.truct std::_Bind_simple(std::basic_string)>?
/usr/include/c++/4.7/thread:133:9:   required from ?.td::thread::thread(_Callable&&, _Args&& ...) [with _Callable = main()::; _Args = {std::basic_string, std::allocator >&}]?
test.cpp:12:7:   required from here
/usr/include/c++/4.7/type_traits:1834:9: error: no match for call to ?.main()::) (std::basic_string)?
test.cpp:9:17: note: candidates are:
In file included from /usr/include/c++/4.7/bits/move.h:57:0,
                 from /usr/include/c++/4.7/bits/stl_pair.h:61,
                 from /usr/include/c++/4.7/bits/stl_algobase.h:65,
                 from /usr/include/c++/4.7/bits/char_traits.h:41,
                 from /usr/include/c++/4.7/ios:41,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from test.cpp:1:
/usr/include/c++/4.7/type_traits:1834:9: note: void (*)(std::string&) {aka void (*)(std::basic_string&)} 
/usr/include/c++/4.7/type_traits:1834:9: note:   candidate expects 2 arguments, 2 provided
test.cpp:9:33: note: main()::
test.cpp:9:33: note:   no known conversion for argument 1 from ?.td::basic_string?.to ?.td::string& {aka std::basic_string&}?
#include 
#include 
#include 

int main()
{
    std::string a("Hello");
    std::cout << "address = " << &a << "\n";
    std::thread t([](std::string& a)
        {
            std::cout << "in thread address = " << &a << "\n";
        }, std::ref(a));
    t.join();
}

转帖:


C++使用Lambda函数实现多线程
thread - 传递引用参数

你可能感兴趣的:(C++使用Lambda函数实现多线程)