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

Kylix_XP 2003-07-11 08:16:32
哪位老大能告诉我线程池的C++实现的代码?非常感谢!!!我有JAVA的实现(ThreadPool.java)
...全文
73 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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

15,473

社区成员

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

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