#include class Packet { public: Packet(int i) : in(i) {} int in; }; typedef boost::promise< Packet > packet_promise; typedef boost::shared_ptr< packet_promise > packet_promise_shared_ptr; typedef std::pair< unsigned long, packet_promise_shared_ptr > seq_promise_pair; typedef std::map< unsigned long, packet_promise_shared_ptr > seq_to_promise_map; class Demo { private: boost::thread setvalue_thread_; seq_to_promise_map mymap_; boost::mutex mymap_mutex_; unsigned long counter_; public: Demo() : counter_(10), setvalue_thread_(boost::bind(&Demo::do_calc_in_thread, this)) { } packet_promise_shared_ptr calc() { packet_promise_shared_ptr ptrPromisePacket(new packet_promise()); boost::mutex::scoped_lock l(mymap_mutex_); mymap_.insert(seq_promise_pair(counter_, ptrPromisePacket) ); counter_++; return ptrPromisePacket; } //calculate in a seperate thread and set_value(), loops forever void do_calc_in_thread() { while(true) { { boost::mutex::scoped_lock l(mymap_mutex_); seq_to_promise_map::iterator value_in_map_iter = mymap_.begin(); for (value_in_map_iter; value_in_map_iter != mymap_.end(); value_in_map_iter++) { unsigned long in_counter = value_in_map_iter->first; //printf("[%d] thread running set value\n", in_counter); Packet resultPacket(in_counter); value_in_map_iter->second->set_value(resultPacket); //make a copy mymap_.erase(value_in_map_iter); } } } } }; int main(int argc, char** argv) { Demo demo; unsigned long testCounter=0; while(true) { packet_promise_shared_ptr packetPromisePtr = demo.calc(); boost::system_time const timeout = boost::get_system_time() + boost::posix_time::milliseconds(500); boost::unique_future< Packet > pf = packetPromisePtr->get_future(); //often causes core-dump if (!pf.timed_wait_until(timeout)) { printf("[%d] error \n", testCounter); break; } else { Packet result = pf.get(); printf("[%d] ok : %d \n", testCounter, result.in); } testCounter++; } }