Chinaunix首页 | 论坛 | 博客
  • 博客访问: 149480
  • 博文数量: 27
  • 博客积分: 531
  • 博客等级: 一等列兵
  • 技术积分: 332
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-25 18:31
文章分类

全部博文(27)

文章存档

2015年(4)

2014年(3)

2013年(6)

2012年(14)

我的朋友

分类: C/C++

2014-04-19 19:20:28

最近一直在使用java,习惯了其中的一些基础设施,其中感触比较深的是它提供的Thread实现,使用起来很是方便。
最为一个对c++情有独钟的人,深深的觉得c++也需要这样的设施。在之前读zmq的代码的时候读到过thread的实现,不过和我想要的东西还有一点差别,所以对其进行了一点改进,所以就有了如下的代码:

thread.hpp

点击(此处)折叠或打开

  1. #ifndef _THREAD_HPP_
  2. #define _THREAD_HPP_

  3. #include <pthread.h>

  4. namespace util
  5. {
  6.     class thread_t
  7.     {
  8.     public:
  9.         inline thread_t() {};
  10.         virtual ~thread_t() {};

  11.         void start();
  12.         void stop();

  13.         virtual void run() = 0;

  14.     private:
  15.         pthread_t tid;

  16.         void error_assert(int rc, const char* msg);

  17.         thread_t(const thread_t&);
  18.         const thread_t& operator = (const thread_t&);
  19.     };
  20. }

  21. #endif
thread.cpp

点击(此处)折叠或打开

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include "thread.hpp"

  4. extern "C"
  5. {
  6.     static void* thread_routine(void* args)
  7.     {
  8.         util::thread_t* self = (util::thread_t*)args;
  9.         self->run();
  10.         return NULL;
  11.     }
  12. }

  13. void util::thread_t::start()
  14. {
  15.     int rc = pthread_create(&tid, NULL, thread_routine, this);
  16.     error_assert(rc, "create thread error");
  17. }

  18. void util::thread_t::stop()
  19. {
  20.     int rc = pthread_join(tid, NULL);
  21.     error_assert(rc, "join thread error");
  22. }

  23. void util::thread_t::error_assert(int rc, const char* msg)
  24. {
  25.     if (rc != 0) {
  26.         perror(msg);
  27.         abort();
  28.     }
  29. }
test.cpp

点击(此处)折叠或打开

  1. // **********************************************************
  2. //
  3. // compile: g++ thread.cpp test.cpp -lpthread -Wall -o test
  4. //
  5. // **********************************************************

  6. #include <cstdio>
  7. #include <cstring>
  8. #include <unistd.h>
  9. #include "thread.hpp"

  10. class mythread_t : public util::thread_t
  11. {
  12. public:
  13.     mythread_t(const char* name) {
  14.         this->name = new char[32];
  15.         strncpy(this->name, name, strlen(name));
  16.     }

  17.     ~mythread_t() {
  18.         delete []name;
  19.     }

  20.     void run() {
  21.         printf("this is %s thread.\n", name);
  22.         sleep(1);
  23.     }

  24. private:
  25.     char* name;
  26. };

  27. int main(int argc, char* argv[])
  28. {
  29.     mythread_t* t1 = new mythread_t("thread-1");
  30.     mythread_t* t2 = new mythread_t("thread-2");
  31.     mythread_t* t3 = new mythread_t("thread-3");

  32.     t1->start();
  33.     t2->start();
  34.     t3->start();

  35.     t1->stop();
  36.     t2->stop();
  37.     t3->stop();

  38.     delete t1;
  39.     delete t2;
  40.     delete t3;

  41.     printf("over\n");

  42.     return 0;
  43. }
thread_t实现了thread框架,test.cpp展示了怎么使用,mythread_t继承自thread_t,实现自己的run方法。
代码已经说完了我想说的东西,但是还是想发一下感慨,java的很多思想还是很有用的。




阅读(1800) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~