Ejemplo n.º 1
0
void CThreadPool::wait() {
    lock();
    while (!busyList.empty()) {
        struct timespec timeoutBusy;
        timeoutBusy.tv_sec = time(NULL) + 10;
        timeoutBusy.tv_nsec = 0;
        pthread_cond_timewait(&release_cond, get_mutex(), &timeoutBusy);
    }
    for (std::list<pthread_t>::iterator iter = idleList.begin(); iter != idleList.end(); iter++) {
        pthread_join(*iter, NULL);
    }
    unlock();
}
Ejemplo n.º 2
0
CThreadWorker* CThreadPool::getWorker() {
    struct timespec timeoutWorker;
    timeoutWorker.tv_sec = time(NULL) + timeout;
    timeout.tv_nsec = 0;
    lock();
    if (workerSet.empty()) {
        pthread_cond_timewait(&worker_cond, get_mutex(),&timeoutWorker);
        std::cout << "worker queue empty" << std::endl;
        unlock();
        return NULL;
    }
    CThreadWorker* worker = workerSet.top();
    workerSet.pop();
    unlock();
    return worker;
}
Ejemplo n.º 3
0
void* Thread::startHook(void* arg)
{
	Thread* data = static_cast<Thread*>(arg);
	pthread_cond_timewait()
	data->m_pRunner->run();
}
Ejemplo n.º 4
0
		void TimerWorker::run()
		{
			log_info("thread run");

			bool lock_hold = false;

			while(!stop) {
				if (!lock_hold) {
					pthread_mutex_lock(&mutex);
					lock_hold = true;
				} // end of if(!lock_hold)

				if (task_list.empty()) {
					pthread_cond_wait(&cond, &mutex);
					continue;
				}

				timespec current_time;
				clock_gettime(CLOCK_REALTIME, &current_time);
				
				//get the first task
				const Task& task = task_list.front();
				
				// current time > task's next_run_time
				// need to run
				if (!timespec_less(current_time, task.next_run_time)) {
					Task to_run = task_list.front();
					task_list.pop_front();
					running_task_id = to_run.task_id;
					//release the lock asap
					pthread_mutex_unlock(&mutex);
					lock_hold = false;
					
					// run user-define processing callback funtion
					// with user-define args
					to_run.task_func_routine(to_run.arg);

					// check whether it is a loop task
					if (to_run.interval > 0) {
						to_run.next_run_time = microsecond_from_now(to_run.interval);
						pthread_mutex_lock(&mutex);
						lock_hold = true;
						
						// if unscheduled task
						if ( running_task_id != 0) {
							running_task_id = 0;
							std::list<Task>::iterator it = upper_bound(task_list.begin,
									task_list.end(), to_run, task_less);	
							task_list.insert(it, to_run);
						} else {
							// if task has been unscheduled
							// do not need to insert it back
						}
					}

					continue;
				} else {
					// wait its task next run time
					// sleep util next run time
					pthread_cond_timewait(&cond, &mutex, task.next_run_time);
					continue;
				}
			}// end of while

			// exit from while
			if (lock_hold) {
				pthread_mutex_unlock(&mutex);
			}
		}