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\n";
    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\n";
    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

 

posted on 2021-04-14 16:16  tycoon3  阅读(51)  评论(0)    收藏  举报

导航