美文网首页
C++使用Lambda函数实现多线程

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

作者: chjxidian | 来源:发表于2020-09-09 17:03 被阅读0次
    #include <iostream>
    #include <thread>
    #include <vector>
    #include <algorithm>
    
    int main()
    {
        // vector 容器存储线程
        std::vector<std::thread> 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 <iostream>
    #include <thread>
    #include <string>
    
    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<false, false, main()::<lambda(std::string&)>, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >?.
    /usr/include/c++/4.7/type_traits:1857:12:   required from ?.lass std::result_of<main()::<lambda(std::string&)>(std::basic_string<char>)>?
    /usr/include/c++/4.7/functional:1563:61:   required from ?.truct std::_Bind_simple<main()::<lambda(std::string&)>(std::basic_string<char>)>?
    /usr/include/c++/4.7/thread:133:9:   required from ?.td::thread::thread(_Callable&&, _Args&& ...) [with _Callable = main()::<lambda(std::string&)>; _Args = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >&}]?
    test.cpp:12:7:   required from here
    /usr/include/c++/4.7/type_traits:1834:9: error: no match for call to ?.main()::<lambda(std::string&)>) (std::basic_string<char>)?
    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<char>&)} <conversion>
    /usr/include/c++/4.7/type_traits:1834:9: note:   candidate expects 2 arguments, 2 provided
    test.cpp:9:33: note: main()::<lambda(std::string&)>
    test.cpp:9:33: note:   no known conversion for argument 1 from ?.td::basic_string<char>?.to ?.td::string& {aka std::basic_string<char>&}?
    
    #include <iostream>
    #include <thread>
    #include <string>
    
    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函数实现多线程

          本文链接:https://www.haomeiwen.com/subject/evzeektx.html