C++ pass multithread via lambda and pass multiple functions and arguments

void Util::thread14(int x,int y,string str)
{
   
    thread t1([](int xx,int yy,string sstr)
    {
        Util ul;
        for(int i=0;i<xx;i++)
        {
            cout<<"Num= "<<i<<","<<ul.getUuid1()<<endl;
        }

        for(int j=0;j<yy;j++)
        {
            cout<<"Uuid="<<ul.getUuid1()<<","<<j<<endl;
        }

        cout<<sstr<<endl; 
    },x,y,str);
    t1.join();
    cout<<getTimeNow()<<",finished in void Util::thread14(int x,int y,string str)"<<endl;
}


char *Util::uuidValue = (char *)malloc(40);
char *Util::dtValue = (char *)malloc(20);

char *Util::getUuid1()
{
    uuid_t newUUID;
    uuid_generate(newUUID);
    uuid_unparse(newUUID, uuidValue);
    return uuidValue;
}

char *Util::getTimeNow()
{
    time_t rawTime = time(NULL);
    struct tm tmInfo = *localtime(&rawTime);
    strftime(dtValue, 20, "%Y%m%d%H%M%S", &tmInfo);
    return dtValue;
}

void thread6(int x,int y,string str);

int main(int args, char **argv)
{
     thread6(atoi(argv[1]),atoi(argv[2]),argv[3]);
}

void thread6(int x,int y,string str)
{
    Util ul;
    ul.thread14(x,y,str);
}
g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -luuid -lpthread
./h1 10 20 "Multithread in C++"

 

posted @ 2022-04-10 13:42  FredGrit  阅读(28)  评论(0编辑  收藏  举报