`

Linux下面实现C++向对象多线程

阅读更多
/*
* Thread.h
*
*  Created on: 2008-10-13
*      Author: root
*/
#ifndef THREAD_H_
#define THREAD_H_
#include <unistd.h>
#include <pthread.h>
class Runnable
{
public:
//运行实体
virtual void run() = 0;
};
//线程类
class Thread: public Runnable
{
private:
//线程初始化号
static int thread_init_number;
//当前线程初始化序号
int current_thread_init_number;
//线程体
Runnable *target;
//当前线程的线程ID
pthread_t tid;
//线程的状态
int thread_status;
//线程属性
pthread_attr_t attr;
//线程优先级
sched_param param;
//获取执行方法的指针
static void* run0(void* pVoid);
//内部执行方法
void* run1();
//获取线程序号
static int get_next_thread_num();
public:
//线程的状态-新建
static const int THREAD_STATUS_NEW = 0;
//线程的状态-正在运行
static const int THREAD_STATUS_RUNNING = 1;
//线程的状态-运行结束
static const int THREAD_STATUS_EXIT = -1;
//构造函数
Thread();
//构造函数
Thread(Runnable *target);
//析构
~Thread();
//线程的运行体
void run();
//开始执行线程
bool start();
//获取线程状态
int get_state();
//等待线程直至退出
void join();
//等待线程退出或者超时
void join(unsigned long millis_time);
//比较两个线程时候相同,通过current_thread_init_number判断
bool operator ==(const Thread* other_pthread);
//获取this线程ID
pthread_t get_thread_id();
//获取当前线程ID
static pthread_t get_current_thread_id();
//当前线程是否和某个线程相等,通过tid判断
static bool is_equals(Thread* iTarget);
//设置线程的类型:绑定/非绑定
void set_thread_scope(bool isSystem);
//获取线程的类型:绑定/非绑定
bool get_thread_scope();
//设置线程的优先级,1-99,其中99为实时,意外的为普通
void set_thread_priority(int priority);
//获取线程的优先级
int get_thread_priority();
};
int Thread::thread_init_number = 1;
inline int Thread::get_next_thread_num()
{
return thread_init_number++;
}
void* Thread::run0(void* pVoid)
{
Thread* p = (Thread*) pVoid;
p->run1();
return p;
}
void* Thread::run1()
{
thread_status = THREAD_STATUS_RUNNING;
tid = pthread_self();
run();
thread_status = THREAD_STATUS_EXIT;
tid = 0;
pthread_exit(NULL);
}
void Thread::run()
{
if (target != NULL)
{
  (*target).run();
}
}
Thread::Thread()
{
tid = 0;
thread_status = THREAD_STATUS_NEW;
current_thread_init_number = get_next_thread_num();
pthread_attr_init(&attr);
}
Thread::Thread(Runnable *iTarget)
{
target = iTarget;
tid = 0;
thread_status = THREAD_STATUS_NEW;
current_thread_init_number = get_next_thread_num();
pthread_attr_init(&attr);
}
Thread::~Thread()
{
pthread_attr_destroy(&attr);
}
bool Thread::start()
{
return pthread_create(&tid, &attr, run0, this);
}
inline pthread_t Thread::get_current_thread_id()
{
return pthread_self();
}
inline pthread_t Thread::get_thread_id()
{
return tid;
}
inline int Thread::get_state()
{
return thread_status;
}
void Thread::join()
{
if (tid > 0)
{
  pthread_join(tid,NULL);
}
}
void Thread::join(unsigned long millis_time)
{
if (tid == 0)
{
  return;
}
if (millis_time == 0)
{
  join();
}
else
{
  unsigned long k = 0;
  while (thread_status != THREAD_STATUS_EXIT && k <= millis_time)
  {
   usleep(100);
   k++;
  }
}
}
bool Thread::operator ==(const Thread* other_pthread)
{
if(other_pthread==NULL)
{
  return false;
}if(current_thread_init_number==(*other_pthread).current_thread_init_number)
{
  return true;
}
return false;
}
bool Thread::is_equals(Thread* iTarget)
{
if (iTarget == NULL)
{
  return false;
}
return pthread_self() == iTarget->tid;
}
void Thread::set_thread_scope(bool isSystem)
{
if (isSystem)
{
  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
}
else
{
  pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
}
}
void Thread::set_thread_priority(int priority)
{
pthread_attr_getschedparam(&attr,&param);
param.__sched_priority = priority;
pthread_attr_setschedparam(&attr,&param);
}
int Thread::get_thread_priority(){
pthread_attr_getschedparam(&attr,&param);
return param.__sched_priority;
}
#endif /* THREAD_H_ */

以为newmain.cpp代码
/*
* newmain.cpp
*
*  Created on: 2008-10-13
*      Author: root
*/
#include "Thread.h"
#include <iostream>
class MutilThread: public Thread
{
public:
Thread* th1;
Thread* th2;
void Test()
{
  th1 = new Thread(this);
  th1->set_thread_priority(90);
  th2 = new Thread(this);
  start();
  th1->start();
  th2->start();
  th1->join();
  th2->join();
}
void run()
{
  if (Thread::is_equals(th1))
  {
   int number = 100;
   for (int i = 0; i < 10; i++)
   {
    std::cout << "this thread1 number is: " << number++
      << std::endl;
    std::cout << "\t pid is: " << getpid() << " tid is "
      << get_current_thread_id() << std::endl;
    sleep(1);
   }
  }
  else if (Thread::is_equals(th2))
  {
   int number = 200;
   for (int i = 0; i < 10; i++)
   {
    std::cout << "this is thread2 number is: " << number++
      << std::endl;
    std::cout << "\t pid is: " << getpid() << " tid is "
      << get_current_thread_id() << std::endl;
    sleep(1);
   }
  }
  else if (Thread::is_equals(this))
  {
   int number = 300;
   for (int i = 0; i < 10; i++)
   {
    std::cout << "this is thread0 number is: " << number++
      << std::endl;
    std::cout << "\t pid is: " << getpid() << " tid is "
      << get_current_thread_id() << std::endl;
    sleep(1);
   }
  }
}
};
int main(int argc, char **argv) {
bool ret;
MutilThread *mt;
mt = new MutilThread();
mt->Test();
return (EXIT_SUCCESS);
}

 

分享到:
评论

相关推荐

    C++面向对象多线程编程.part1.rar

    本书基于windows和unix like(unix及linux等类unix),从面向对象的角度深入讨论了多线程实现以及应该注意的问题,确实是一本初学c++或正想向多线程或者说跨平台方向发展的程序员学习的好书。

    C++面向对象多线程编程(多操作系统版)

    本书基于windows和unix like(unix及linux等类unix),从面向对象的角度深入讨论了多线程实现以及应该注意的问题,确实是一本初学c++或正想向多线程或者说跨平台方向发展的程序员学习的好书。

    Linux多线程服务端编程:使用muduo C++网络库

    《Linux多线程服务端编程:使用muduo C++网络库》主要讲述采用现代C++在x86-64 Linux上编写多线程TCP网络服务程序的主流常规技术,重点讲解一种适应性较强的多线程服务器的编程模型,即one loop per thread。...

    C++教程网《Linux网络编程》视频百度云地址

    Linux网络编程之线程篇 Linux网络编程之TCP/IP基础篇 01TCPIP基础(一) ISO/OSI参考模型 TCP/IP四层模型 基本概念(对等通信、封装、分用、端口) 02TCPIP基础(二) 最大传输单元(MTU)/路径MTU 以太网帧...

    Linux系统设计-听说C与Linux更搭配:C基础 C++面向对象编程 , linux系统编程以及一些操作系统的相关知识

    Linux系统是一个免费使用和自由传播的类Unix操作系统,基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统,Linux是许多企业...

    一些C++的资料

    7. 深入浅出Win32多线程程序设计 8. C++面向对象程序设计-谭浩强 9. linux从入门到精通教程pdf完整版 10.Linux开发指南 11.Linux网络编程 12.Qt编程及应用经典教程 13.史上最牛的Linux内核学习方法论

    linux环境下C++实现俄罗斯方块

    本文实例为大家分享了C++实现俄罗斯方块的具体代码,供大家参考,具体内容如下 本程序的运行环境是linux,用到了多线程。创建了一个用来绘图的线程和一个获取按键的线程。程序中有一些需要改善的地方,比如336-338行...

    [免费]2018年C++教程网的linux网络编程视频百度云下载链接.rar

    Linux网络编程之线程篇 Linux网络编程之TCP/IP基础篇 01TCPIP基础(一) ISO/OSI参考模型 TCP/IP四层模型 基本概念(对等通信、封装、分用、端口) 02TCPIP基础(二) 最大传输单元(MTU)/路径MTU 以太网帧...

    2018年C++教程网的linux网络编程视频共41集百度云下载链接.rar

    Linux网络编程之线程篇 Linux网络编程之TCP/IP基础篇 01TCPIP基础(一) ISO/OSI参考模型 TCP/IP四层模型 基本概念(对等通信、封装、分用、端口) 02TCPIP基础(二) 最大传输单元(MTU)/路径MTU 以太网帧格式 ...

    jdk1.8-最新版-linux-32位CSDN下载

    Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。 本资源主要是Lnux下jdk-8u281-linux-...

    c++教程网的linux网络编程视频下载

    Linux网络编程之线程篇 Linux网络编程之TCP/IP基础篇 01TCPIP基础(一) ISO/OSI参考模型 TCP/IP四层模型 基本概念(对等通信、封装、分用、端口) 02TCPIP基础(二) 最大传输单元(MTU)/路径MTU 以太网帧...

    Herm(一套快速开发高性能的网络应用的C++库)

    抽象了Win32 Select、Linux epoll和FreeBSD kqueue的多路复用API。统一了三者水平模式(Level Triggered)的语义(一套代码在Win32/Linux/FreeBSD运行结果是一样的),Linux上也支持了边缘模式(Edge Triggered)。 ...

    C++开发:为什么多线程读写shared_ptr要加锁的详细介绍

    我在《Linux 多线程服务端编程:使用 muduo C++ 网络库》第 1.9 节“再论 shared_ptr 的线程安全”中写道: (shared_ptr)的引用计数本身是安全且无锁的,但对象的读写则不是,因为 shared_ptr 有两个数据成员,...

    C++毕业设计:基于Qt的多线程局域网聊天系统(含客户端+服务端).zip

    而Qt又是基于C++一种语言的扩展,大家都知道C++ 有快速、简易、面向对象等很多优点,所以Qt自然也继承者C++这些的优点。 Qt良好的封装机制使得Qt的模块化程度非常高,可重用性较好,对用户开发来货是非常方便的。Qt...

    C++设计模式

    , , 完成本书的学习后,您将可以创建多线程GUI应用程序,这些应用程序可以访问数据库和操作XML文件,当然更具有吸引力的是它们可以运行在包括Windows、Linux、Unix和Mac OSX在内的多个平台之上!最令人高兴的是您...

    C++教程网视频:linux网络编程

    Linux网络编程之线程篇 Linux网络编程之TCP/IP基础篇 01TCPIP基础(一) ISO/OSI参考模型 TCP/IP四层模型 基本概念(对等通信、封装、分用、端口) 02TCPIP基础(二) 最大传输单元(MTU)/路径MTU 以太网帧...

    log4cplus 源码(C++编写的开源的日志系统)

    log4cplus具有线程安全、灵活、以及多粒度控制的特点,通过将信息划分优先级使其可以面向程序调试、运行、测试、和维护等全生命周 期; 你可以选择将信息输出到屏幕、文件、 NT event log、甚至是远程服务器;通过...

    thinking in c++

    ·介绍被认为是标准C++下一版特征之一的多线程处理编程技术,并提供最新研究成果 ·对书中包含的所有示例代码都提供免费下载,这些代码段经过多个软件平台和编译器(包括基于Windows/Mac/Linux的GNU C++编译器) ...

    清华大学Linux操作系统原理与应用

    D.2 Linux上的C/C++编译器和调试器 238 D.2.1 运行gcc/egcs 238 D.2.2 gcc/egcs的主要选项 240 D.2.3 gdb简介 240 D.2.4 gdb的常用命令 241 D.2.5 gdb使用示例 242 D.3 GNU make和makefile 243 D.3.1 GNU make 243 D...

    基于Qt Creator实现中国象棋人机对战, c++实现.zip

    Signal & Slot机制是Qt中实现对象间事件驱动通信的核心方式。信号代表对象状态变化或事件发生,槽则是响应这些信号的可调用实体。这种松耦合的通信方式简化了异步编程和事件处理。 QML与Qt Quick: QML是一种...

Global site tag (gtag.js) - Google Analytics