lambdas in a thread

#include <iostream>
#include <thread>

int main()
{
    int id = 9;
    std::thread t([&id](){
        std::cout << "thread function and id " << id << std::endl;
    });
    std::cout << "main thread
";
    t.join();
    return 0;
}
root@ubuntu:~/c++# g++ -std=c++11 -pthread threa1.cpp -o thread1
root@ubuntu:~/c++# ./thread1 
main thread
thread function and id 9
#include <iostream>
#include <thread>
#include <string>

int main()
{
    int id = 9;
    std::thread t([&id](std::string name){
        std::cout << "thread function and id " << id << " and name " << name << std::endl;
    }, "dirk");
    std::cout << "main thread
";
    t.join();
    return 0;
}
root@ubuntu:~/c++# g++ -std=c++11 -pthread threa1.cpp -o thread1
root@ubuntu:~/c++# ./thread1
main thread
thread function and id 9 and name dirk
原文地址:https://www.cnblogs.com/dream397/p/14658495.html