设为首页 加入收藏

TOP

一种简单易用的C++线程类
2015-07-20 17:19:43 来源: 作者: 【 】 浏览:3
Tags:简单 易用 线程

一 代码结构

\

二 代码

1. threadobject.h

?

/*************************************************************************
    > File Name: threadobject.h
    > Author: wangzhicheng
    > Mail: 2363702560@qq.com 
    > Created Time: Sat 07 Feb 2015 10:13:33 PM WST
 ************************************************************************/
#ifndef THREADOBJECT_www.2cto.com
#define THREADOBJECT_H
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         using namespace std; /* *abstract thread class * */ class Threadobject { private: pthread_t mTid; string mName; protected: virtual void do_something(void) = 0; // the real function which thread need to execute public: Threadobject(); ~Threadobject(); void setname(const string &); const string& getname() const; void run(void); void join(void); friend void *thread(void *); }; /* * user thread class * */ class Userthread:public Threadobject { protected: virtual void do_something(void); public: Userthread(); ~Userthread(); }; #endif
       
      
     
    
   
  

?

2. threadobject.cpp

?

/*************************************************************************
    > File Name: threadobject.cpp
    > Author: wangzhicheng
    > Mail: 2363702560@qq.com 
    > Created Time: Sat 07 Feb 2015 10:32:14 PM WST
 ************************************************************************/
#include "threadobject.h"
void *thread(void *arg);
Threadobject::Threadobject() {
}
Threadobject::~Threadobject() {
}
void Threadobject::run() {
	pthread_create(&this->mTid, NULL, thread, this);
}
void Threadobject::join() {
	pthread_join(this->mTid, NULL);
}
void Threadobject::setname(const string &name) {
	this->mName = name;
}
const string& Threadobject::getname() const {
	return this->mName;
}
void *thread(void *arg) {
	Threadobject *thread = static_cast
  
   (arg);
	thread->do_something();

	return NULL;
}
Userthread::Userthread() {
}
Userthread::~Userthread() {
}
void Userthread::do_something(void) {
	cout << this->getname() << endl;
}
  

3. main.cpp

?

?

/*************************************************************************
    > File Name: main.cpp
    > Author: wangzhicheng
    > Mail: 2363702560@qq.com 
    > Created Time: Sun 08 Feb 2015 07:53:38 PM WST
 ************************************************************************/
#include "threadobject.h"
int main() {
	Userthread userthread1, userthread2;
	userthread1.setname("userthread1");
	userthread2.setname("userthread2");
	userthread1.run();
	userthread2.run();
	userthread1.join();
	userthread2.join();

	return 0;
}

4. makfile

?

?

CC=g++
all:
	$(CC) -g -o main main.cpp threadobject.cpp threadobject.h -lpthread

三 程序运行截图

?

\

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇BZOJ 1009 HNOI 2008 GT考试 AC自.. 下一篇LeetCode --- 41. First Missing ..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·【C语言】动态内存管 (2025-12-27 06:23:20)
·C语言中的内存管理 - (2025-12-27 06:23:16)
·C语言指南:C语言内 (2025-12-27 06:23:14)
·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)