Domanda

Can you please help me translate the following methods from the pthread library to boost library :

pthread_t tid; // I guess this should become : boost::thread tid; 
//... some instructions ...

pthread_create(&tid, NULL, (void*(*)(void*))process_request, (void*)some_ptr); 

void *process_request(void *soap) 
{ 
   pthread_detach(pthread_self()); 
   //... some instructions ...
   return NULL; 
}

Thank you a lot!

È stato utile?

Soluzione

It's simply:

boost::thread(&process_request, some_ptr).detach();

You can replace boost by std and your program is portable in c++11.

Note that process_request can actually take the parameter strong-typed now. If you want to pass a reference, use boost::ref (or std::ref, indeed):

void process_request(MyData& data)
{
}

MyData data;
boost::thread th(&process_request, boost::ref(data));
th.join();

It's a bit of a pattern in C++11, though, to move the data into the thread function, so as to avoid races on the parameters being passed:

void process_request(MyData&& data)
{
}

MyData data;
boost::thread th(&process_request, std::move(data));
th.join();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top