From: Edd Dawson (lists_at_[hidden])
Date: 2008-01-04 21:52:34
I'm currently trying to write tests for some threaded code. However, context
switching is not predictable and scheduling is unfair on the platforms
concerned, so there is no deterministic behaviour on which I can rely.
Here's a sketch of my task_queue class:
class task_queue : noncopyable
// use num_threads threads to churn through tasks
// Don't start any new tasks.
// Return as soon as all active tasks are complete.
~task_queue(); // throw()
// Add a task to the back of the queue
void append(const function<void ()> &task);
The tasks should be picked up in FIFO order. Without some kind of deterministic
threading implementation, I can't test this with the given interface. To truly
test FIFO ordering, looking at when tasks start and/or finish is insufficient.
Instead I need to see when a task is picked up by a thread. I can hack in some
hooks, but that seems wrong(?).
So! I had this slightly wacky thought. Would it be possible to implement the
boost.Thread API using Boost.Coroutine? Coroutine yield()s would be performed
inside blocking calls to whatever_lock_type::lock(), thread::join(),
condition::wait() and so on, perhaps; I haven't completely thought this through,
If this is possible, I would have a deterministic threading system and therefore
have some guarantees to work with in my tests.
For example, I can now use the starting time of a task as a measure of the time
at which a worker thread plucks a task from the queue (because there would be no
context switching between the pluck and the first instruction of the task).
Thus, I can test FIFO-ness.
I realise Boost.Coroutine isn't an official Boost library, but I'm guessing this
is still the best place to find out whether this idea has wings. Hope that's ok.
Laugh me off stage if you must!
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk