求线程池的代码(C++实现)!

Kylix_XP 2003-07-11 08:16:32
哪位老大能告诉我线程池的C++实现的代码?非常感谢!!!我有JAVA的实现(ThreadPool.java)
...全文
65 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengge8ylf 2003-07-24
  • 打赏
  • 举报
回复
study
Kylix_XP 2003-07-24
  • 打赏
  • 举报
回复
http://www.codeproject.com/threads/threads.asp
上面的线程不能用..无法使用呀..
哪位大哥知道更好的线程池实现呀?俺感激不尽....
harrypotter 2003-07-19
  • 打赏
  • 举报
回复
study
yongyu2000 2003-07-13
  • 打赏
  • 举报
回复
windows核心编程里,好像有一章,专门讲这个
Kylix_XP 2003-07-13
  • 打赏
  • 举报
回复
还是老外好,都是活雷锋...

Kylix_XP 2003-07-13
  • 打赏
  • 举报
回复
to realdreamer(楼主英明,贫僧久仰大名,特来拜见)

为什么不能给呢?难道是什么机密吗? ThreadPool.h

// ThreadPool.h: interface for the CThreadPool class.
// Rajeev Sadasivan, Trivandrum, INDIA
// 12-Dec-2002
// mailto: rajeevcs_in@yahoo.com
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_THREADPOOL_H__653A92C6_53BE_42B9_90BD_26F5DC231A56__INCLUDED_)
#define AFX_THREADPOOL_H__653A92C6_53BE_42B9_90BD_26F5DC231A56__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#pragma warning( disable: 4786 )
#include <queue>
using namespace std ;
#pragma warning( default: 4786 )

/* ------------------------------ USAGE ------------------------------ >>
1. Insert ThreadPool.cpp & .h in project.
2. Project Settings
- On the Project menu, Click Settings.
- In the Project Settings dialog box, click the C/C++ tab.
- Select Code Generation from the Category drop-down list box.
- From the Use Run-Time Library drop-down box, select MULTITHREADED.
- Click OK.
3. Constuct - Max threads, tasks can be recommended. Tasks list size
will grow if required.
4. Call Initialize() with required thread count.
5. Assign Task with a _cdecl function pointer of the client.
6. Function will be executed from any of the free thread in thread pool.
7. If DATA members need to be sync, in the submitted function in client,
that has to be done by the client.
8. When thread count need to be increased/decreased, just call Initialize again.
NO Resource/Memory LEAK. The current threads finishes executuion.
Then it will be aborted.
9. To destroy the thread pool, without deleting threadpool object,
call UnInitialize(). This will delete all resources. If threads are still
working, after WM_QUIT posting & a small waiting, it terminates all threads.
10. UnInitialize() is called from destructor() also.
<< ------------------------------ USAGE ---------------------------
*/

// -------Common External Data structures---------------

// e.g. bool MyThreadPoolCallback(UINT nFunctionIdentifier, LPVOID pMyData)
typedef bool (*THREAD_POOL_EXECUTE_FN)(UINT, LPVOID);

typedef struct _tagTHREAD_POOL_TASK
{
UINT unMessage; // Task Id; Can be 0.
THREAD_POOL_EXECUTE_FN pFunction; // Execution Procedure provided by client.
LPVOID pExtraInfo; // Extra information.
}
THREAD_POOL_TASK, *PTHREAD_POOL_TASK;

// --------------------------------------------

// --- Private Data structures --- >>

#define THREADPOOL_MAX_THREAD_COUNT 24
#define THREADPOOL_MAX_TASK_COUNT 1024

typedef struct _tagTHREAD_POOL_QUEUE_INFO
{
THREAD_POOL_TASK FuncInfo;
UINT ixTask;
} THREAD_POOL_QUEUE_INFO, * PTHREAD_POOL_QUEUE_INFO;

typedef unsigned int (__stdcall* THREAD_PROC)(void *);

typedef struct _tagTHREAD_DATA
{
unsigned long hThread; // Thread handle.
unsigned int nThreadId; // Thread Id.
unsigned int nFlags; // Thread status such as marked for exit.
}
THREAD_DATA, *PTHREAD_DATA;

class CThreadPool
{
queue<PTHREAD_POOL_QUEUE_INFO> m_TaskQueue; // Task queue.
int *m_aFreeIndex;
THREAD_DATA * m_aThreadData;
UINT m_cntThreads;
PTHREAD_POOL_QUEUE_INFO m_aTasks; // Task Data block.
UINT m_ixNextTask;
HANDLE m_hTaskQueueSync;
HANDLE m_hClientSync;

public: // Static methods.
static unsigned int __stdcall WorkerThreadProc( void * pInfo);

public: // Public Methods.
CThreadPool();
virtual ~CThreadPool();
bool Initialize( UINT cntThreads=1/*Default value*/,
UINT nMaxThreadCount= THREADPOOL_MAX_THREAD_COUNT,
UINT nMaxTaskCount= THREADPOOL_MAX_TASK_COUNT );
void Uninitialize();
bool AssignTask(PTHREAD_POOL_TASK pTask);
void WorkerThreadProc();

private: // Data
UINT m_cntRemove;
UINT m_cntMaxThread;
UINT m_cntMaxTasks;

private: // Methods
void NotifyAll(UINT nMsg, WPARAM wParam, LPARAM lParam);
void FreeThreadDataBlock(UINT ixThread);
UINT GetMyThreadDataIndex();
void RemoveIfMarked();
void MarkThreadsForDelete(UINT cntThrds);
void AllocNewThreads(UINT cntThrds);
};

#endif // !defined(AFX_THREADPOOL_H__653A92C6_53BE_42B9_90BD_26F5DC231A56__INCLUDED_)



realdreamer 2003-07-12
  • 打赏
  • 举报
回复
有也不能给你. 还是两个线程池, 一个在 win32 通用, 一个应用于winnt/2000 服务器用.
Kylix_XP 2003-07-11
  • 打赏
  • 举报
回复
谢谢 Skt32(荒城之月)

源文件能在CBuilder6下编译不能通过..请问怎么在CBuilder调用这个ThreadPool类呢?
Skt32 2003-07-11
  • 打赏
  • 举报
回复
A Thread Pool compatible with Win32 and pthreads API
By Thomas George
A cross-platform thread pooling implementation
http://www.codeproject.com/threads/threads.asp
Skt32 2003-07-11
  • 打赏
  • 举报
回复
The Code Project - A thread pool implementation - Threads, ...
http://www.codetools.com/threads/CPThreadPool001.asp
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧