Re: MT Design Question
"Paavo Helde" <myfirstname@osa.pri.ee> wrote in message
news:Xns9DDFEF607DD9Cmyfirstnameosapriee@216.196.109.131...
[...]
If there is a way to wait for multiple future<> results, all this could
be done by the master thread instead, which can then sort out all this
ternary logic by itself. This might be a bit cleaner, especially in
regard of dealing with orphaned threads (those not yet finished when the
master thread returns the found result).
This pseudo-code will only work when a future is obtained from a promise:
________________________________________________________
struct complete
{
std::condition_variable m_cond;
std::mutex m_mutex;
};
template<typename T>
struct promise_ex
{
std::promise<T> m_promise;
complete& m_complete;
void set_value(T v)
{
m_complete.m_mutex.lock();
m_promise.set_value(v);
m_complete.m_mutex.unlock();
m_complete.m_cond.notify_one();
}
};
template<typename T>
struct multi_wait
{
complete m_complete;
linked_list<std::future<T>*> m_list;
void push_future(std::future<T>* f)
{
m_list.push(f);
}
std::future<T>* wait()
{
std::future<T>* result = NULL;
m_complete.m_mutex.lock();
while (! m_list.is_empty())
{
for each std::future<T>* in m_list as f
{
if (f->is_ready())
{
result = f;
m_list.pop(f);
goto bailout;
}
}
m_complete.m_cond.wait(m_complete.m_mutex);
}
bailout:
m_complete.m_mutex.unlock();
return result;
}
};
________________________________________________________
That should allow a thread to wait on the result of multiple futures without
spin-polling.