Exemple #1
1
void WorkItemQueue::Process()
{
	while (!m_dying) {
		WorkItem* item = NULL;
		boost::unique_lock<boost::mutex> lock(m_mutex);
		while ((!m_dying) && (item = Pop())) {
			try {
				//                LslDebug( "running WorkItem %p, prio = %d", item, item->m_priority );
				item->Run();
			} catch (std::exception& e) {
				// better eat all exceptions thrown by WorkItem::Run(),
				// don't want to let the thread die on a single faulty WorkItem.
				LslDebug("WorkerThread caught exception thrown by WorkItem::Run -- %s", e.what());
			} catch (...) {
				LslDebug("WorkerThread caught exception thrown by WorkItem::Run");
			}
			CleanupWorkItem(item);
		}
		// cleanup leftover WorkItems
		while ((item = Pop()) != NULL) {
			CleanupWorkItem(item);
		}
		if (!m_dying)
			//wait for the next Push
			m_cond.wait(lock);
	}
}
void* WorkerThread::Entry()
{
	WorkItem* item;

	wxLogMessage( _T( "WorkerThread started" ) );

	while ( !TestDestroy() ) {
		// sleep an hour or until a new WorkItem arrives (DoWork() will wake us up).
		Sleep( 3600 * 1000 );
		while ( !TestDestroy() && ( item = m_workItems.Pop() ) != NULL ) {
			try {
				wxLogMessage( _T( "running WorkItem %p, prio = %d" ), item, item->m_priority );
				item->Run();
			}
			catch ( ... ) {
				// better eat all exceptions thrown by WorkItem::Run(),
				// don't want to let the thread die on a single faulty WorkItem.
				wxLogMessage( _T( "WorkerThread caught exception thrown by WorkItem::Run" ) );
			}
			CleanupWorkItem( item );
			// give other threads some air
			Yield();
		}
	}

	// cleanup leftover WorkItems
	while ( ( item = m_workItems.Pop() ) != NULL ) {
		CleanupWorkItem( item );
	}

	wxLogMessage( _T( "WorkerThread stopped" ) );
	return 0;
}