Timeout a TBB pipeline filter
<p>This is what I'm trying to achieve:</p>

<p>I'm using <code>TBB's</code> pipeline for processing. I have several filters in the pipeline and I need the filters to be as fast as possible since this is a real-time application. One of the filters can sometime take longer than I can afford it to, so I want to somehow set a timeout on that specific filter.</p>

<p>After looking for a while I found the following solution, which works in general but has its issues:</p>

<p>When the filter is created, I create an event <code>HANDLE</code></p>

<pre><code>m_Event = CreateEvent(NULL, FALSE, FALSE, NULL);
</code></pre>

<p>Then, the filter calls a function using async and waits on the event</p>

<pre><code>...
auto funcBind = std::bind(&amp;MyFunc, ...)
auto function = std::async(std::launch::async, funcBind, m_Event ...); 
long res = WaitForSingleObject(m_Event, delayMS);
auto myFuncRes = function.get()
if (res == WAIT_OBJECT_0 &amp;&amp; (bool)myFuncRes)
{
    // MyFunc Finished successfuly
}
else if (res == WAIT_TIMEOUT)
{
    // Timeout expired
}
else
{
    // MyFunc failed
}
...
return;
</code></pre>

<p>Just before the function <code>MyFunc</code> returns, it signals the event</p>

<pre><code>SetEvent(event);
</code></pre>

<p>So now, if the <code>delayMS</code> timeout expires, the TBB filter doesn't delay the entire pipeline. The problem is that <code>MyFunc</code> still runs in the background, it doesn't stop.</p>

<p>My question is, Is there a better way to set a timeout on a TBB filter, and is there a solution to stopping <code>MyFunc</code>'s execution once a timeout was reached (I'd prefer not to use timers or timing checks inside it, maybe somehow to use the event and check if it was taken by the filter or something)</p>