多线程编程基础(线程创建)
2012-01-30 16:53
267 查看
1、多线程的创建
线程的创建比较简单,先举一个例子热热身:
解决方案有三个:
1)通过一个非成员函数包装一下这个类,非成员函数的参数是类的对象,有了类的对象就可一调用类的成员函数了,示例代码如下:
2)当然这个非成员函数也可以是类的静态成员函数,那么结果将是下面这个样式:
3)另线程创建的工作在类的内部完成,使得类具有对立运行的性质,熟悉java的同学对下面的实现一定非常熟悉:
有了这个ThreadBase基类,再创建支持多线程的类就非常容易了,只要继承并实现Run函数就可以了。
参考文献:
https://computing.llnl.gov/tutorials/pthreads/
http://www.dutor.net/index.php/2011/11/pthread-in-cpp-class/
http://stackoverflow.com/questions/1151582/pthread-function-from-a-class
线程的创建比较简单,先举一个例子热热身:
#include <stdio.h> #include <pthread.h> void* Handler(void* param) { char* message = static_cast<char*>(param); printf("%s\n", message); } int main(int argc, char** argv) { pthread_t thread_id; char message[] = "hello world"; pthread_create(&thread_id, NULL, Handler, message); void* ret = NULL; pthread_join(thread_id, &ret); }有时候,我们需要将以个类的成员函数作为线程的routine函数使用,但pthread_create不能接收一个非静态的成员函数作为参数,因此类的成员函数必须是静态的,但这样的静态成员函数使得访问类的非静态数据成员会有问题,因此这不是一个很好的解决方案,例如下面的程序,ThreadBase类的设计和实现会有很多不便。
#include <stdio.h> #include <pthread.h> class ThreadBase { public: static void* Handler(void* param) { char* message = static_cast<char*>(param); printf("%s\n", message); } }; int main(int argc, char** argv) { pthread_t thread_id; char message[] = "hello world"; pthread_create(&thread_id, NULL, ThreadBase::Handler, message); void* ret = NULL; pthread_join(thread_id, &ret); }
解决方案有三个:
1)通过一个非成员函数包装一下这个类,非成员函数的参数是类的对象,有了类的对象就可一调用类的成员函数了,示例代码如下:
#include <stdio.h> #include <pthread.h> #include <string> class ThreadBase { public: void SetMessage(const char* message) { message_ = message; } void Handler() { printf("%s\n", message_.c_str()); } private: std::string message_; }; void* ThreadCall(void* object) { ThreadBase* thread_base = static_cast<ThreadBase*>(object); thread_base->Handler(); } int main(int argc, char** argv) { pthread_t thread_id; char message[] = "hello world"; ThreadBase thread_base; thread_base.SetMessage(message); pthread_create(&thread_id, NULL, ThreadCall, &thread_base); void* ret = NULL; pthread_join(thread_id, &ret); }
2)当然这个非成员函数也可以是类的静态成员函数,那么结果将是下面这个样式:
#include <stdio.h> #include <pthread.h> #include <string> class ThreadBase { public: void SetMessage(const char* message) { message_ = message; } void Handler() { printf("%s\n", message_.c_str()); } static void* ThreadCall(void* object) { ThreadBase* thread_base = static_cast<ThreadBase*>(object); thread_base->Handler(); } private: std::string message_; }; int main(int argc, char** argv) { pthread_t thread_id; char message[] = "hello world"; ThreadBase thread_base; thread_base.SetMessage(message); pthread_create(&thread_id, NULL, ThreadBase::ThreadCall, &thread_base); void* ret = NULL; pthread_join(thread_id, &ret); }
3)另线程创建的工作在类的内部完成,使得类具有对立运行的性质,熟悉java的同学对下面的实现一定非常熟悉:
#include <stdio.h> #include <pthread.h> #include <string> class ThreadBase { public: virtual ~ThreadBase() {} void SetMessage(const char* message) { message_ = message; } void Start() { pthread_create(&thread_id_, NULL, Hook, this); } void* Join() { void* ret = NULL; pthread_join(thread_id_, &ret); return ret; } virtual void Run() { printf("%s\n", message_.c_str()); } private: static void* Hook(void* object) { ThreadBase* thread_base= static_cast<ThreadBase*>(object); thread_base->Run(); } pthread_t thread_id_; std::string message_; }; class ThreadDerived : public ThreadBase { public: virtual void Run() { printf("a new derived multi-thread object is running\n"); } }; int main(int argc, char** argv) { pthread_t thread_id; char message[] = "hello world"; ThreadBase thread_base; thread_base.SetMessage(message); thread_base.Start(); thread_base.Join(); ThreadDerived thread_derived; thread_derived.Start(); thread_derived.Join(); }
有了这个ThreadBase基类,再创建支持多线程的类就非常容易了,只要继承并实现Run函数就可以了。
参考文献:
https://computing.llnl.gov/tutorials/pthreads/
http://www.dutor.net/index.php/2011/11/pthread-in-cpp-class/
http://stackoverflow.com/questions/1151582/pthread-function-from-a-class
相关文章推荐
- linux基础编程:多线程基础总结:线程创建 终止 线程同步/互斥量/条件变量/信号灯
- 多线程编程基础(线程创建)
- C# 多线程编程基础——怎么创建和执行多个线程
- 多线程基础之线程创建
- Java多线程编程基础之线程对象
- C# 多线程编程 - 使用委托创建线程
- 多线程编程-线程的创建和终止
- 多线程编程-线程的创建和终止
- VC多线程编程之线程创建与示例
- 【原创】《Linux高级程序设计》杨宗德著 - Linux多线程编程 - 线程概念及创建线程 分类: Linux --- 应用程序设计 2014-11-19 17:31 82人阅读 评论(0) 收藏
- 多线程编程-线程的创建和终止
- 多线程编程-线程的创建和终止
- Linux下多线程编程__线程的创建pthread_create与退出pthread_exit,等待当前线程退出pthread_join,获取线程ID pthread_self
- Unix高级编程:线程基础、线程的创建、退出、分离、汇合、同步问题
- Java多线程基础学习之线程的创建方式总结
- clone的fork与pthread_create创建线程有何不同&pthread多线程编程的学习小结
- Java基础-多线程-①线程的创建和启动
- 【原创】《Linux高级程序设计》杨宗德著 - Linux多线程编程 - 线程概念及创建线程
- 多线程编程-线程的创建和终止
- 多线程基础知识第一篇:创建线程的方式及线程常用的方法