Christopher Pisz <
christo...@gmail.com> writes:
>I don't think there is any cancel mechanism, only signal. So, what do I do in the following scenario (I try to describe best I can)?
>
>There is a blocking call in existing code that expects a value to be returned for a given string.
>
>There is an asynchronous networked library that can get that value for the given string.
>
>So, I created a condition variable in the former that gets signaled when a map is updated in the latter.
>
Add a "terminate" flag and check it when the condition variable wait
function returns.
#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
void
CLASSNAME::thread_run_method(void)
{
ignore_signals();
g_lock.lock();
while (!ACCESS_ONCE(g_terminate)) {
g_fifoevent.wait(&g_lock);
if (ACCESS_ONCE(g_terminate)) break;
c_dlist_iterator di(&g_upstream_queue);
while (di.next()) {
s_wqe *wqep = (s_wqe *)di.curr();
process_message(wqep->f_core, wqep->f_message);
wqep->remove();
delete wqep;
}
}
g_lock.unlock();
}
(g_fifoevent is an instance of c_condition, an abstraction of the pthread_cond_*
functionality, g_lock is an instance of c_lock, an abstraction of pthread_mutex_*).