Monday 26 April, 2010

Scheduling work in multithreaded environment

Its always better to schedule any work at the end of the thread execution as the scheduled work can delete the object all together.
Consider a scenario like
fn Query::readData()
if(m_readDataSize > 0)
Query object needs some data to evaluate and thus called getData(). But, this getData() can do scheduling to read more data or may place the already read data size in m_readDataSize or it can schedule work to delete the object because of any error case like improper query.
And, if so, there is chance of code getting crashed while accessing m_readDataSize as object might got deleted if the work got scheduled and executed before returning from this function call. So, better is to mark the work to be scheduled and schedule it at the end of the thread execution, say, something like thread->postWork().
With this in view, I always like the OSs which are single threaded like Netware. It gives the better performance, flexibility in using CPU, ease in programming (Ofcourse, it have its own challenges like yielding.)

No comments: