C++ thread lambda expression contains multiple function and pass argument

void thread28(int num,string sstr)
{
    thread t([](int x,string str)
             {
                 printNumUuid26();
                 printUuidNum27(); 
                 printUuid30(x);
                 printUuid31(str);
              },num,sstr);
    t.join();
}

void printUuid31(string str)
{
    cout<<str<<endl;
    cout<<"FILE:"<<__FILE__<<",FUNCTION:"<<__FUNCTION__<<",LINE:"<<__LINE__<<endl;
}

void printUuid30(int num)
{
    for(int i=0;i<num;i++)
    {
        cout<<num<<","<<getUuid1()<<endl;
    }
}

void printUuid29()
{
    for(int i=0;i<10;i++)
    {
        cout<<getUuid1()<<","<<getUuid1()<<endl;
    }
}

void printNumUuid26()
{
    for (int i = 0; i < 10; i++)
    {
        cout << "N=" << i << ",value=" << getUuid1() << endl;
    }
}

void printUuidNum27()
{
    for (int i = 0; i < 10; i++)
    {
        cout << "U=" << getUuid1() << ",Num=" << i << endl;
    }
}

int main(int args, char **argv)
{
    try
    {
        thread28(atoi(argv[1]),argv[2]);
    }
    catch (const std::exception &e)
    {
        cout << e.what() << endl;
        std::cerr << e.what() << '\n';
    }
    return 0;
}
g++ -g -std=c++2a -I. *.cpp  -o h1 -luuid -lpthread
/h1 20 'Test thread pass multiple functions and arguments via lambda expression'

 

 

posted @ 2022-04-03 19:01  FredGrit  阅读(32)  评论(0编辑  收藏  举报