noncopyable.h

#ifndef __WD_NONCOPYABLE_H__
#define __WD_NONCOPYABLE_H__ namespace wd
{ class Noncopyable
{
protected:
Noncopyable(){}
~Noncopyable(){}
private:
Noncopyable(const Noncopyable&);
Noncopyable & operator=(const Noncopyable &);
}; }//end of namespace wd #endif

thread.h

#ifndef __WD_MUTEXLOCK_H__
#define __WD_MUTEXLOCK_H__

#include "Noncopyable.h"
#include <pthread.h>
namespace wd
{ class MutexLock
:public Noncopyable //表达语义
{
public:
MutexLock()
{ pthread_mutex_init(&_mutex, NULL); } ~MutexLock()
{ pthread_mutex_destroy(&_mutex); } void lock()
{ pthread_mutex_lock(&_mutex);} void unlock()
{ pthread_mutex_unlock(&_mutex);} pthread_mutex_t * getMutexLockPtr()
{ return &_mutex; } private:
pthread_mutex_t _mutex;
}; //RAII
class MutexLockGuard
{
public:
MutexLockGuard(MutexLock & mutex)
: _mutex(mutex)
{ _mutex.lock(); } ~MutexLockGuard()
{ _mutex.unlock(); } private:
MutexLock & _mutex;
}; }//end of namespace wd #endif

  

thread.cc

#include "Thread.h"
#include <iostream> using std::cout;
using std::endl;
using namespace wd; Thread::Thread()
: _pthid(0)
, _isRunning(false)
{} void Thread::start()
{
pthread_create(&_pthid, NULL, threadFunc, this);
_isRunning = true;
} void * Thread::threadFunc(void * arg)
{
Thread * pthread = static_cast<Thread*>(arg);
if(pthread)
pthread->run();// 执行任务 return NULL;
} void Thread::join()
{
pthread_join(_pthid, NULL);
_isRunning = false;
} Thread::~Thread()
{
if(_isRunning)
{
pthread_detach(_pthid);// 将运行的线程交给系统进行托管
_isRunning = false;
}
}

  

testThread.cc

#include "Thread.h"

#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::unique_ptr; class MyThread
: public wd::Thread
{
void run()
{
::srand(::time(NULL));
int cnt = 10;
while(cnt--) {
int number = ::rand() % 100;
cout << ">> Thread "<< pthread_self() << " get a number : " << number << endl;
::sleep(1);
}
}
}; int main(void)
{
cout << "MainThread: " << pthread_self() << endl;
unique_ptr<wd::Thread> myThread(new MyThread());//线程对象在主线程
myThread->start();
myThread->join(); return 0;
}

  

最新文章

  1. UVa 524 Prime Ring Problem(回溯法)
  2. 多栏多列布局(display:flex)
  3. LeetCode 387. First Unique Character in a String
  4. IOS BLE蓝牙4.0
  5. 昨晚把家里的ie升级到11
  6. 利用.htaccess绑定子域名到子目录(亲测万网可用)
  7. 微信公共服务平台开发(.Net 的实现)11-------客服消息(定项消息推送 重要的OPENID)
  8. c++ RAII 资源管理就是初始化
  9. 摆方块(贪心)P1087
  10. DRAM与NAND Flash产业六大趋势预测分析
  11. HTML5 实现拍照上传
  12. C#遍历指定文件夹中的所有文件(转)
  13. 一分钟学会JavaMail(假)__手动滑稽
  14. docker 部署mvc项目 &lt;四&gt;
  15. linux之egrep命令
  16. 大数据学习笔记4 - Hadoop的优化与发展(Hadoop 2.0)
  17. DataOutputStream and DataInputStream
  18. websockect外网无法访问问题
  19. epoch、 iteration和batchsize区别
  20. java代码求阶乘n!

热门文章

  1. POJ 2985 Treap平衡树(求第k大的元素)
  2. Ubuntu/CentOS下编译Nginx最基本参数
  3. Micro Python:运行在微控制器上的Python
  4. Python中文编码过程中遇到的一些问题
  5. DBUtils使用详解
  6. android菜鸟学习笔记4----android项目结构
  7. JAVA数据类型(转)
  8. mysql系列之5.mysql备份恢复
  9. 我的Java开发学习之旅------>Java NIO 报java.nio.charset.MalformedInputException: Input length = 1异常
  10. re.sub用法