⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread.cpp

📁 LINUX下很好的C++线程池源码类
💻 CPP
字号:
// Thread.cpp: implementation of the Thread class.
//
//////////////////////////////////////////////////////////////////////

#include "thread.h"
#ifndef  WIN32
#include <unistd.h>
#endif
unsigned long  __thread_proc(void *p) {
	((Thread*)p)->run();
	return 0;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Thread::Thread( ThreadPool *  pPool )
{
	m_fquit = false;
	// bool Create(unsigned long (*start_address)(void*), void* p)
	//m_thread.Create( __thread_proc, this);
	m_pthreadPool = pPool ;
	

}

bool Thread::Create(){
     return m_thread.Create( __thread_proc, this);
}

Thread::~Thread()
{
	while (!m_fquit) 
	{ 
		MThread::Sleep(80); 
	}
	
}

void Thread::run()
{
	while (true) {

	//pthread_cond_wait(get_threadpool().get_object(), &m_mutex);
		
		if (get_threadpool()->is_stop() ) {
			break;
		}
		if (get_threadpool()->is_pause() ) {
			MThread::Sleep(3000);
			continue;
		}	
		ThreadJobPtr pjob = get_threadpool()->get_next();
		while (pjob != NULL) {
			pjob->execute();
			if(pjob->IsRetJob()){
				get_threadpool()->add_done(pjob);		
			}
			pjob = get_threadpool()->get_next();
		}
	}
	m_fquit = true;
}


//Thread* Thread::create_thread(  )
//{
//	long threads = get_threadcount(false);
//	if (threads < ->get_threadcount()) {
//		Thread* pthread = new Thread(pPool);
//		get_threadpool().add_thread(pthread);
//		get_threadcount(true);
//		return pthread;
//	}
//	return NULL;
//}

//long Thread::get_threadcount(bool finc, bool finit)
//{
//	static long nthreads;
//	if (finit)
//		nthreads = 0;
//	if (finc) {
//		nthreads++;
//	}
//	return nthreads;
//}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -