将创建线程的API-pthread_create封装成一个线程类

本文介绍如何将pthread_create多线程API封装到一个线程类中,以便更方便地创建和管理线程。通过创建线程对象,可以实现对线程的封装和抽象。

之前写的多线程都是在主进程中调用线程的API,这里为每一个线程封装成一个类;创建了一个线程对象,也就是创建了一个线程;


MyThread.h

/*************************************************************************
	> File Name: MyThread.h
	> Author: 
	> Mail: 
	> Created Time: 2015年12月14日 星期一 16时23分17秒
 ************************************************************************/

#ifndef _MYTHREAD_H
#define _MYTHREAD_H

#include <iostream>
#include <pthread.h>
#include <string>

using std::string;

class MyThread{
    public:
        MyThread();
        ~MyThread();
        void set_name(string name);
        string get_name();
        pthread_t get_pid();
        void run(void *(*fun)(void *), void *arg);
        void join();
        void do_something();//not use
        friend void * thread_routine(void *arg);//not use
    private:
        pthread_t pid;
        string thread_name;
};

#endif

MyThread.cpp

/*************************************************************************
	> File Name: MyThread.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月14日 星期一 16时31分43秒
 ************************************************************************/

#include "MyThread.h"
using namespace std;

MyThread::MyThread()
{
    
}

MyThread::~MyThread()
{
    pthread_join(pid, NULL);
}

void MyThread::set_name(string name)
{
    thread_name = name;
}

string MyThread::get_name()
{
    return thread_name;
}

pthread_t MyThread::get_pid()
{
    return pid;
}
void MyThread::run(void *(*fun)(void *), void *arg)
{
    pthread_create(&pid, NULL, fun, arg);
}

void MyThread::join()
{
    pthread_join(pid, NULL);
}

/*not use*/
void MyThread::do_something()
{
   cout << "thread id is " << pid << ", name is " << thread_name << endl;
}

/*not use*/
void * thread_routine(void *arg)
{
    MyThread * pthread;
    pthread = static_cast<MyThread *>(arg);
    pthread->do_something();

    return NULL;
}

main.cpp

/*************************************************************************
	> File Name: main.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月14日 星期一 16时13分21秒
 ************************************************************************/

#include <cstdlib>
#include <string.h>
#include "MyThread.h"

using namespace std;

void *thread_fun(void * arg)
{
    cout << "this is thread routine..." <<  endl;

    return NULL;
}


int main(int argc, char *argv[])
{
    int i = 0, num = 0;

    if (argc != 2){
        cout << "input the number of thread object you want to create...." << endl;
        exit(-1);
    }
    
    num = atoi(argv[1]);
    MyThread thread[num];
    
    cout << "I will create " << num << " threads." << endl;
    for (i = 0; i < num; ++i){
        thread[i].set_name("thread");
    }

    for(i = 0; i < num; ++i){
        thread[i].run(thread_fun, NULL);
        cout << thread[i].get_name() << " pid is " << thread[i].get_pid() <<  " is running.." << endl;
    } 

    for(i = 0; i < num; ++i){
        thread[i].join();
    } 
    
    cout << "main thread exit....." << endl;

    exit(0);
}



``` typedef struct { pthread_mutex_t hLock; volatile int iIsRun; void* (* volatile pfnTask)(void*); //函数指针 void *arg; //任务参数 }Task; typedef struct { pthread_mutex_t StackLock; int iIDStack[MAX_TASK_NUM]; int iTopPointer; }Stack; static Stack g_IDTaskStack; static Task stTaskQueueAry[MAX_TASK_NUM]; //static int iTaskCount = 0; //static pthread_mutex_t stMutexQueue; //static pthread_cond_t stCondQueue; static pthread_t stThreadAry[cmnDfn_WORKING_PTHREAD_NUMBER]; void* PthreadPoolWorker(void* arg) { int iRet = -1; int iTskID = (int)(long)arg; Task *pstTsk = &stTaskQueueAry[iTskID]; while(g_iExit == 0) { if (pstTsk->iIsRun == 0) { sleep(1); continue; } iRet = (int)(long)pstTsk->pfnTask(pstTsk->arg); if(iRet < 0) { break; } if (iRet == 0) { continue; } LOCK(pstTsk->hLock) { pstTsk->pfnTask = NULL; pstTsk->iIsRun = 0; } LOCK(g_IDTaskStack.StackLock) { g_IDTaskStack.iIDStack[++g_IDTaskStack.iTopPointer] = iTskID; } } iRet = 0; return (void *)(long)iRet; } int ThreadPoolSubmit(void* (*task)(void*),void* arg,int *piTskID) { int iTskID = 0; if(g_IDTaskStack.iTopPointer < 0) { return -1; } LOCK(g_IDTaskStack.StackLock) { iTskID = g_IDTaskStack.iIDStack[g_IDTaskStack.iTopPointer--]; assert(iTskID >= 0 && iTskID < MAX_TASK_NUM); } LOCK(stTaskQueueAry[iTskID].hLock) { stTaskQueueAry[iTskID].pfnTask = task; stTaskQueueAry[iTskID].arg = arg; stTaskQueueAry[iTskID].iIsRun = 1; *piTskID = iTskID; } return 0; } int ThreadPoolInit(void) { int iLoopNum; int iRet = -1; int iTskID = 0; g_IDTaskStack.iTopPointer = -1; if(pthread_mutex_init(&g_IDTaskStack.StackLock,NULL) < 0) { cmn_PrtError("Error in initializing stack mutex"); } for(iLoopNum = 0; iLoopNum < cmnDfn_WORKING_PTHREAD_NUMBER; iLoopNum++) { g_IDTaskStack.iIDStack[++g_IDTaskStack.iTopPointer] = iLoopNum; } for(iLoopNum = 0; iLoopNum < cmnDfn_WORKING_PTHREAD_NUMBER; iLoopNum++) { if(pthread_mutex_init(&stTaskQueueAry[iLoopNum].hLock,NULL) < 0) { cmn_PrtError("Error in initializing mutex"); } if(pthread_create(&stThreadAry[iLoopNum],NULL,PthreadPoolWorker,(void *)(long)iTskID) != 0) { cmn_PrtError("Error in creating thread"); } stTaskQueueAry[iLoopNum].pfnTask = NULL; stTaskQueueAry[iLoopNum].iIsRun = 0; iTskID++; } iRet = 0; _Exit: return iRet; } int ThreadPoolDestroy(void) { int iLoopNum; int iRet = -1; g_iExit = 1; for(iLoopNum = 0; iLoopNum < cmnDfn_WORKING_PTHREAD_NUMBER; iLoopNum++) { pthread_join(stThreadAry[iLoopNum], NULL); } for(iLoopNum = 0; iLoopNum < cmnDfn_WORKING_PTHREAD_NUMBER; iLoopNum++) { pthread_mutex_destroy(&stTaskQueueAry[iLoopNum].hLock); } iRet = 0; return iRet; }```如何使用这个线程池程序实现UDP协议下多个客户端和服务端的持续数据收发?
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值