Polling
Instead of reacting to notifications from the thread, we can also periodically check the status of background calculation from the main thread. In other words, we poll their status.
If we want to send some data from a background worker, we can simply insert it into a TLockedList<T> or, for a faster operation, into TThreadedQueue<T>. In the main thread, we must then periodically check if something has appeared in this queue and process the data.
As the TThreadedQueue<T> has quite an unwieldy user interface which doesn't allow us to check whether the queue is empty, we have to take care while creating it. The following code initializes the queue with two parameters (queue size—we know that we'll only put two values in the queue), ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access