• 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'

  • 相关阅读:
    605
    603
    509
    7-5
    6-5
    5-6
    5-3
    5-4
    5-5
    4-12
  • 原文地址:https://www.cnblogs.com/Fred1987/p/16096985.html
Copyright © 2020-2023  润新知