void loadingThreadMain() { while (true) { // wait until alerted condition.wait(); // lock, and copy into a temporary queue queue temp_queue; lock_resource_queue(); temp_queue.push( resource_queue.pop() ); unlock_resource_queue(); for each (i in temp_queue) load( i ); }}// and in the main thread:void loadResource() { // lock/add the resource lock_resource_queue(); resource_queue.push( resource ); unlock_resource_queue(); // notify the other thread condition.notify();}
However, suppose the ResourceThread is past the point of copying to the temp queue and loadResource() is called. A resource will be safely pushed onto the resource_queue, but condition.notify() will have no effect because the resource loading queue has not yet gotten to condition.wait(). This would be fixed if I constantly check (using a lock) in the resource thread if the resource_queue has anything, but this seems inefficient and also requires a ton of locking (basically constant locking/unlocking). What am I missing here?