代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
线程管理与封装
作者:
Dezai.CN
/ 发布于
2011/8/31
/
558
在进行编程时,难免会进行多线程的操作,win32 api使用起来诸多不便,因此我用C++封装了一下。 包含: 线程基类Thread 线程组管理ThreadManager 临界区锁CriticalSection 当然实际运用中不可能只有这点,大家根据需要自己加代码完成吧。比如事件锁,互斥锁,信号锁等等
#include <windows.h> #include <iostream> #include <string> #include <vector> #include <process.h> #include <assert.h></div> <div>// Thread.h --------------------------------------</div> <div>/* 线程封装 */ class Thread { public: typedef unsigned long handle_t; typedef unsigned ( __stdcall * start_routine )( void * param ); bool want_stop; bool auto_delete; unsigned thread_id; unsigned exit_code;</div> <div>Thread( void ); virtual ~Thread( void ); virtual void run() = 0;</div> <div>void start( void ); void pause( void ); void close( void ); void wait( unsigned long ms = INFINITE ); void forceStop( void );</div> <div>handle_t handle( void ) { return _thread; } private: handle_t _thread; static unsigned __stdcall _start_run( Thread * thread_ptr );</div> <div>Thread( Thread const & ); Thread & operator = ( Thread const & ); };</div> <div>// Thread.cpp ------------------------------------ Thread::Thread( void ) : _thread(0), thread_id(0), want_stop(false), auto_delete(true), exit_code(0) { _thread = _beginthreadex( NULL, 0, (start_routine)_start_run, this, CREATE_SUSPENDED, &this->thread_id ); } <div>Thread::~Thread( void ) { this->close(); } <div>void Thread::start( void ) { ResumeThread( (HANDLE)_thread ); } <div>void Thread::pause( void ) { SuspendThread( (HANDLE)_thread ); } <div>void Thread::close( void ) { if ( _thread ) { CloseHandle( (HANDLE)_thread ); _thread = 0; } } <div>void Thread::wait( unsigned long ms /*= INFINITE */ ) { WaitForSingleObject( (HANDLE)_thread, ms ); } <div>unsigned __stdcall Thread::_start_run( Thread * thread_ptr ) { thread_ptr->run(); unsigned exit_code = thread_ptr->exit_code;</div> if ( thread_ptr->auto_delete ) delete thread_ptr;</div> <div>return exit_code; } <div>void Thread::forceStop( void ) { TerminateThread( (HANDLE)_thread, exit_code ); } <div>// ThreadManager.h ---------------------------------------- /* 线程管理器 */ template <typename _Thread> class ThreadManager { public: typedef typename std::vector<_Thread *> thread_array;</div> <div>ThreadManager( void ) {} ~ThreadManager( void ) { thread_array::iterator it; for ( it = _thread_arr.begin(); it != _thread_arr.end(); it++ ) { delete *it; } _thread_arr.clear(); } template <typename _ARG_T1> void create( int count, _ARG_T1 arg1 ) { int i; for ( i = 0; i < count; i++ ) { _Thread * th_ptr = new _Thread(arg1); th_ptr->auto_delete = false; _thread_arr.push_back(th_ptr); } } template < typename _ARG_T1, typename _ARG_T2 > void create( int count, _ARG_T1 arg1, _ARG_T2 arg2 ) { int i; for ( i = 0; i < count; i++ ) { _Thread * th_ptr = new _Thread( arg1, arg2 ); th_ptr->auto_delete = false; _thread_arr.push_back(th_ptr); } } template < typename _ARG_T1, typename _ARG_T2, typename _ARG_T3> void create( int count, _ARG_T1 arg1, _ARG_T2 arg2, _ARG_T3 arg3 ) { int i; for ( i = 0; i < count; i++ ) { _Thread * th_ptr = new _Thread( arg1, arg2, arg3 ); th_ptr->auto_delete = false; _thread_arr.push_back(th_ptr); } } void create( int count ) { int i; for ( i = 0; i < count; i++ ) { _Thread * th_ptr = new _Thread(); th_ptr->auto_delete = false; _thread_arr.push_back(th_ptr); } } void start( void ) { thread_array::iterator it; for ( it = _thread_arr.begin(); it != _thread_arr.end(); it++ ) { (*it)->start(); } } void pause( void ) { thread_array::iterator it; for ( it = _thread_arr.begin(); it != _thread_arr.end(); it++ ) { (*it)->pause(); } } void close( void ) { thread_array::iterator it; for ( it = _thread_arr.begin(); it != _thread_arr.end(); it++ ) { (*it)->close(); } } void wait( unsigned long ms = INFINITE ) { int count = _thread_arr.size(); assert( count <= MAXIMUM_WAIT_OBJECTS ); HANDLE * th_hs = new HANDLE[count]; int i; for ( i = 0; i < count; ++i ) { th_hs[i] = (HANDLE)_thread_arr[i]->handle(); } WaitForMultipleObjects( count, th_hs, TRUE, ms ); delete [] th_hs; } private: thread_array _thread_arr;</div> <div>ThreadManager( ThreadManager const & ); ThreadManager & operator = ( ThreadManager const & ); };</div> <div>// Lock.h ----------------------------------------------------- /* 临界区锁 */ class CriticalSection { public: CriticalSection( void ); ~CriticalSection( void ); void lock( void ); void unlock( void ); bool trylock( void ); private: CRITICAL_SECTION _cs;</div> <div>CriticalSection( CriticalSection const & ); CriticalSection & operator = ( CriticalSection const & ); };</div> <div>// Lock.cpp ----------------------------------------------------- CriticalSection::CriticalSection( void ) { ZeroMemory( &_cs, sizeof(_cs) ); InitializeCriticalSection(&_cs); } <div>CriticalSection::~CriticalSection( void ) { DeleteCriticalSection(&_cs); } <div>void CriticalSection::lock( void ) { EnterCriticalSection(&_cs); } <div>void CriticalSection::unlock( void ) { LeaveCriticalSection(&_cs); } <div>bool CriticalSection::trylock( void ) { return TryEnterCriticalSection(&_cs); } <div>// Test.cpp -------------------------------------------------------- /*自定义一个线程*/ class MyThread : public Thread { public: MyThread( CriticalSection * cs_lock ) : _cs_lock(cs_lock) { } MyThread( CriticalSection * cs_lock, std::string name ) : _cs_lock(cs_lock), _name(name) { } virtual void run() { int i = 0; while ( !this->want_stop && i++ < 100 ) { _cs_lock->lock(); std::cout << _name << " " << this->thread_id << " 正在运行...\n"; _cs_lock->unlock(); } } private: CriticalSection * _cs_lock; std::string _name; };</div> <div> int main() { std::string cmd; CriticalSection cs; class MyThreadOnce : public Thread { public: MyThreadOnce( CriticalSection * cs ) : _cs(cs) {} ~MyThreadOnce( void ) { _cs->lock(); std::cout << this->thread_id << " 析构!\n"; _cs->unlock(); } virtual void run() { _cs->lock(); std::cout << this->thread_id << " 运行完毕!\n"; _cs->unlock(); } private: CriticalSection * _cs; };</div> <div>// 启动一个线程,运行完毕自动析构 (new MyThreadOnce(&cs))->start();</div> <div>// 管理一组线程,调用create()函数创建线程,可最多传3个参数调用构造函数 ThreadManager<MyThread> mgr; mgr.create( 5, &cs ); mgr.create( 5, &cs, std::string("Output") ); mgr.start(); // 接受用户输入 while ( std::cin >> cmd ) { if ( cmd == "stop" ) { break; } } <div> return 0; } </div>
试试其它关键字
线程管理
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3