예제 #1
0
파일: Iocp.cpp 프로젝트: cymheart/server
// 初始化完成端口
bool ServerContext::_InitializeIOCP()
{
	// 建立第一个完成端口
	iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);

	if (NULL == iocp)
	{
		//LOG4CPLUS_ERROR(log.GetInst(), "建立完成端口失败!错误代码:"<<WSAGetLastError());
		return false;
	}

	// 根据本机中的处理器数量,建立对应的线程数
	nThreads = WORKER_THREADS_PER_PROCESSOR * GetNumProcessors();

	// 为工作者线程初始化句柄
	workerThreads = new HANDLE[nThreads];

	// 根据计算出来的数量建立工作者线程
	for (int i = 0; i < nThreads; i++)
	{
		THREADPARAMS_WORKER* pThreadParams = new THREADPARAMS_WORKER;
		pThreadParams->serverCtx = this;
		pThreadParams->nThreadNo = i + 1;

		workerThreads[i] = CreateThread(NULL, 0, _WorkerThread, (void*)pThreadParams, 0, NULL);
	}

	return true;
}
예제 #2
0
int main ()
{
	unsigned int cpu_num =  GetNumProcessors();
	printf("%u\n", cpu_num);
	
	return 0;
}