void gep::TaskWorker::runTasks() { do { while(m_pTaskQueue->m_isRunning && runSingleTask() == SUCCESS) {} } while(m_pTaskQueue->m_isRunning && stealTasks() == SUCCESS); }
void WSCoreBoundTaskQueue::executeTask() { //infinite thread loop while (1) { std::shared_ptr<Task> task; //block protected by _threadStatusMutex { std::lock_guard<std::mutex> lk1(_threadStatusMutex); if (_status == TO_STOP) break; } // lock queue to get task std::unique_lock<std::mutex> ul(_queueMutex); // get task and execute if (_runQueue.size() > 0) { // get first task task = _runQueue.front(); _runQueue.pop_front(); ul.unlock(); if (task) { //LOG4CXX_DEBUG(logger, "Started executing task" << std::hex << &task << std::dec << " on core " << _core); // run task (*task)(); //std::cout << "Executed task " << task->vname() << "; hex " << std::hex << &task << std::dec << " on core " << _core<< std::endl; LOG4CXX_DEBUG(logger, "Executed task " << std::hex << &task << std::dec << " on core " << _core); // notify done observers that task is done task->notifyDoneObservers(); } // no task in runQueue -> try to steal task from other queue, otherwise sleep and wait for new tasks } else { // try to steal work ul.unlock(); if (stealTasks() != NULL) continue; ul.lock(); //if queue still empty go to sleep and wait until new tasks have been arrived if (_runQueue.size() < 1) { { //check if queue was suspended { std::lock_guard<std::mutex> lk1(_threadStatusMutex); if (_status != RUN) continue; } //std::cout << "queue " << _core << " sleeping " << std::endl; _condition.wait(ul); } } } } }
void WSCoreBoundPriorityQueue::executeTask() { //infinite thread loop while (1) { if (_status == TO_STOP) break; // get task and execute std::shared_ptr<Task> task; _runQueue.try_pop(task); // no task in runQueue -> try to steal task from other queue, otherwise sleep and wait for new tasks if(!task) { // try to steal work task = stealTasks(); // WSCoreBoundPriorityQueue based on tbb's queue keeps spinning for the time beeing; // we cannot trust _runQueue.size() and if we implement our own size(), we can use // a mutexed std::priority_queue /* if (!task){ //if queue still empty go to sleep and wait until new tasks have been arrived std::unique_lock<lock_t> ul(_queueMutex); if (_runQueue.size() < 1) { { // if thread is about to stop, break execution loop if(_status != RUN) break; _condition.wait(ul); } } }*/ } if (task) { //LOG4CXX_DEBUG(logger, "Started executing task" << std::hex << &task << std::dec << " on core " << _core); // run task //std::cout << "Running task " << task->vname() << "; hex " << std::hex << &task << std::dec << " on core " << _core<< std::endl; (*task)(); //std::cout << "Executed task " << task->vname() << "; hex " << std::hex << &task << std::dec << " on core " << _core<< std::endl; LOG4CXX_DEBUG(logger, "Executed task " << std::hex << &task << std::dec << " on core " << _core); // notify done observers that task is done task->notifyDoneObservers(); } } }