Thread example using a function object and using the return value
I would like to have a function object that I can use the return value of its operator() function. Here is what I think best explains my request class Apple { public: int operator()( int x ) { int value = x * 2; return value; } } void main (int, char**) { int thread_count = 2; // 2-Core cpu // Create a pool of Apple threads typedef boost::shared_ptrboost::thread Thread_Ptr_t; typedef std::list < Thread_Ptr_t > Thread_Pool_t; Thread_Pool_t m_thread_pool; // Create threads for ( int index = 0; index < thread_count; ++index ) { Thread_Ptr_t new_thread_ptr ( new boost::thread ( boost::bind ( &Apple, _1, )( index ) ) ); m_thread_pool.push_back ( new_thread_ptr ); } // Wait for all threads to finish for ( Thread_Pool_t::iterator pos = m_thread_pool.begin(); pos != m_thread_pool.end(); ++pos ) { (*pos)->join(); } } Where is the point I should be catching return from the operator() function? I want the thread to do work and then have the opportunity to get some results from it. Is there a code example someone can show me? Stephen
Stephen Torri
I would like to have a function object that I can use the return value of its operator() function.
Where is the point I should be catching return from the operator() function? I want the thread to do work and then have the opportunity to get some results from it. Is there a code example someone can show me?
boost::thread discards the return value from the thread function or function object. If you want the return value, you need to use something like the proposed Boost futures library. My implementation is here: http://www.justsoftwaresolutions.co.uk/threading/updated-implementation-of-c... Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL
participants (2)
-
Anthony Williams
-
Stephen Torri