ARCEmu項目wow私服伺服器網路模組分析(四)

來源:互聯網
上載者:User

線程池:

/* * Thread Pool Class * Copyright (C) Burlex <burlex@gmail.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program.  If not, see <http://www.gnu.org/licenses/>. * */#include "../Common.h"#ifndef __THREADPOOL_H#define __THREADPOOL_H#ifdef WIN32class SERVER_DECL ThreadController{public:HANDLE hThread;uint32 thread_id;void Setup(HANDLE h){hThread = h;// whoops! GetThreadId is for windows 2003 and up only! :< - Burlex//thread_id = (uint32)GetThreadId(h);}void Suspend(){// We can't be suspended by someone else. That is a big-no-no and will lead to crashes.ASSERT(GetCurrentThreadId() == thread_id);SuspendThread(hThread);}void Resume(){// This SHOULD be called by someone else.ASSERT(GetCurrentThreadId() != thread_id);if(ResumeThread(hThread) == DWORD(-1)){DWORD le = GetLastError();printf("lasterror: %u\n", le);}}void Join(){WaitForSingleObject(hThread, INFINITE);}uint32 GetId() { return thread_id; }};#else#ifndef HAVE_DARWIN#include <semaphore.h>int GenerateThreadId();class ThreadController{sem_t sem;pthread_t handle;int thread_id;public:void Setup(pthread_t h){handle = h;sem_init(&sem, PTHREAD_PROCESS_PRIVATE, 0);thread_id = GenerateThreadId();}~ThreadController(){sem_destroy(&sem);}void Suspend(){ASSERT(pthread_equal(pthread_self(), handle));sem_wait(&sem);}void Resume(){ASSERT(!pthread_equal(pthread_self(), handle));sem_post(&sem);}void Join(){// waits until the thread finishes then returnspthread_join(handle, NULL);}ARCEMU_INLINE uint32 GetId() { return (uint32)thread_id; }};#elseint GenerateThreadId();class ThreadController{pthread_cond_t cond;pthread_mutex_t mutex;int thread_id;pthread_t handle;public:void Setup(pthread_t h){handle = h;pthread_mutex_init(&mutex, NULL);pthread_cond_init(&cond, NULL);thread_id = GenerateThreadId();}~ThreadController(){pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);}void Suspend(){pthread_cond_wait(&cond, &mutex);}void Resume(){pthread_cond_signal(&cond);}void Join(){pthread_join(handle, NULL);}ARCEMU_INLINE uint32 GetId() { return (uint32)thread_id; }};#endif#endifstruct SERVER_DECL Thread{ThreadBase* ExecutionTarget;ThreadController ControlInterface;Mutex SetupMutex;bool DeleteAfterExit;};typedef std::set<Thread*> ThreadSet;class SERVER_DECL CThreadPool{int GetNumCpus();uint32 _threadsRequestedSinceLastCheck;uint32 _threadsFreedSinceLastCheck;uint32 _threadsExitedSinceLastCheck;uint32 _threadsToExit;int32 _threadsEaten;Mutex _mutex;ThreadSet m_activeThreads;ThreadSet m_freeThreads;public:CThreadPool();// call every 2 minutes or so.void IntegrityCheck();// call at startupvoid Startup();// shutdown all threadsvoid Shutdown();// return true - suspend ourselves, and wait for a future task.// return false - exit, we're shutting down or no longer needed.bool ThreadExit(Thread* t);// creates a thread, returns a handle to it.Thread* StartThread(ThreadBase* ExecutionTarget);// grabs/spawns a thread, and tells it to execute a task.void ExecuteTask(ThreadBase* ExecutionTarget);// prints some neat debug statsvoid ShowStats();// kills x free threadsvoid KillFreeThreads(uint32 count);// resets the gobble counterARCEMU_INLINE void Gobble() { _threadsEaten = (int32)m_freeThreads.size(); }// gets active thread countARCEMU_INLINE uint32 GetActiveThreadCount() { return (uint32)m_activeThreads.size(); }// gets free thread countARCEMU_INLINE uint32 GetFreeThreadCount() { return (uint32)m_freeThreads.size(); }};extern SERVER_DECL CThreadPool ThreadPool;#endif

 這還真是suspend,resume 方式的線程池,太貼切了。

可是在windows下,我更喜歡用iocp模型 做線程池,

windows下的線程池,大家有沒有同感?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.