lundi 31 août 2020

fork() vs std::async in C++

I am new to concepts of asyc and fork, what I understood till now is that the fork() creates chid processes which will run asynchronously. And the std::async will create a thread from the system pool and launch it asynchronously here if I mention std::launch::async.

For instance how does below set of code differs? 1: using std::async

std::vector<std::future<void>> result;
for(i=0;i<5;i++){
    result. push_back(std::async(std::launch::asyc, foo, var1));
}

for( auto e : result) 
    e.get();

2nd: Using fork()

for(i=0;i<5;i++){ 
    if(fork()==0){
        foo(var1);
        exit(0);
    }
}

Assume the foo function return type is void, and the arguments are passed as reference or pointers.

Aucun commentaire:

Enregistrer un commentaire