int perthreadtestApp::main (void) { outputOne = outputTwo = outputThree = 0; __THREADED = true; testThread one (&outputOne, 18321); testThread two (&outputTwo, 33510); testThread three (&outputThree, 18495); one.sendevent ("shutdown"); two.sendevent ("shutdown"); three.sendevent ("shutdown"); threadStopped.wait (); ::printf ("%i %i %i\n", outputOne, outputTwo, outputThree); threadStopped.wait (); ::printf ("%i %i %i\n", outputOne, outputTwo, outputThree); threadStopped.wait (); ::printf ("%i %i %i\n", outputOne, outputTwo, outputThree); value out; out.newval() = outputOne; out.newval() = outputTwo; out.newval() = outputThree; out.savexml ("out.xml"); return 0; }
inline void wait() const { mut.lock(); waitercount++; while (semvalue == 0) { cond.wait(mut); } waitercount--; semvalue--; mut.unlock(); }
/** * The conceptual "reverse" of dequeue(). * This function will block until the queue becomes empty, or * until stop_blocking() is called. * Returns true on success. * Returns false if the queue is no longer alive */ bool wait_until_empty() { m_mutex.lock(); // if the queue still has elements in it while I am still alive, wait while (m_queue.empty() == false && m_alive == true) { sleeping_on_empty++; m_empty_conditional.wait(m_mutex); sleeping_on_empty--; } m_mutex.unlock(); // if I am alive, the queue must be empty. i.e. success // otherwise I am dead return m_alive; }
inline bool wait_for_data() { m_mutex.lock(); bool success = false; // Wait while the queue is empty and this queue is alive while(m_queue.empty() && m_alive) { sleeping++; m_conditional.wait(m_mutex); sleeping--; } // An element has been added or a signal was raised if(!m_queue.empty()) { success = true; } m_mutex.unlock(); return success; }
inline std::pair<T, bool> dequeue_and_begin_critical_section_on_success() { m_mutex.lock(); T elem = T(); bool success = false; // Wait while the queue is empty and this queue is alive while(m_queue.empty() && m_alive) { sleeping++; m_conditional.wait(m_mutex); sleeping--; } // An element has been added or a signal was raised if(!m_queue.empty()) { success = true; elem = m_queue.front(); m_queue.pop_front(); if (m_queue.empty() && sleeping_on_empty) { m_empty_conditional.signal(); } } if (!success) m_mutex.unlock(); return std::make_pair(elem, success); }
/** * Waits for all replies to complete. It is up to the * reply implementation to decrement the counter. */ inline void wait() { mut.lock(); while(!valready) cond.wait(mut); mut.unlock(); }