C++线程类实现

#ifndef THREAD_VITO_H
#define THREAD_VITO_H

#ifdef _WIN32
#include<windows.h>
#else
#include <pthread.h>
#endif

class ThreadVito
{

 #ifdef _WIN32
    typedef unsigned(__stdcall* func)(void*);
#else
    typedef void* (* func)(void*);
#endif

public:
    ThreadVito(func p);
    ~ThreadVito();
    void join();
private:
#ifdef _WIN32
HANDLE _handle;
DWORD  _id;
#else
pthread_t _thread;
#endif

bool _started;
bool _running;
};

#endif

 

#include "ThreadVito.h"
#include<assert.h>
#ifdef _WIN32
#include<windows.h>
#include<process.h>
#else
#include <pthread.h>
#endif

ThreadVito::ThreadVito(func startHook)
{
#ifdef _WIN32
    unsigned int id;
    _handle =
        reinterpret_cast<HANDLE>(
            _beginthreadex(nullptr,
                0,
                startHook,
                this,
                0,// 新线程的初始状态,0表示立即执行,CREATE_SUSPENDED表示创建之后挂起
                &id));

    _id = id;
    assert(_handle != (HANDLE)-1L);
    if (_handle == 0)
    {

    }
    /*if (SetThreadPriority(_handle, priority) == 0)
    {
        
    }*/
    
#else
    pthread_attr_t attr;
    int rc = pthread_attr_init(&attr);
    if (rc != 0)
    {
        pthread_attr_destroy(&attr);
    }
    /*if (stackSize > 0)
    {
        if (stackSize < PTHREAD_STACK_MIN)
        {
            stackSize = PTHREAD_STACK_MIN;
        }
#ifdef __APPLE__
        if (stackSize % 4096 > 0)
        {
            stackSize = stackSize / 4096 * 4096 + 4096;
        }
#endif*/
    /*    rc = pthread_attr_setstacksize(&attr, stackSize);
        if (rc != 0)
        {
            pthread_attr_destroy(&attr);
        }*/
//    }
    /*
    if (realtimeScheduling)
    {
        rc = pthread_attr_setschedpolicy(&attr, SCHED_RR);
        if (rc != 0)
        {

        }
        sched_param param;
        param.sched_priority = priority;
        rc = pthread_attr_setschedparam(&attr, &param);
        if (rc != 0)
        {

            pthread_attr_destroy(&attr);
    
        }
        pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
    }
    */
    rc = pthread_create(&_thread, &attr, startHook, this);
    pthread_attr_destroy(&attr);
    if (rc != 0)
    {

    }

#endif
    _started = true;
    _running = true;
}


ThreadVito::~ThreadVito()
{
}

void ThreadVito::join()
{
#ifdef  _WIN32
    if (_handle == 0)
    {

    }
    DWORD rc = WaitForSingleObjectEx(_handle, INFINITE, true);
    if (rc != WAIT_OBJECT_0)
    {

    }
#else
    void* ignore = 0;
    int rc = pthread_join(_thread, &ignore);
    if (rc != 0)
    {
    }
#endif

}

#include "MutexVito.h"
#include "ThreadVito.h"
#include<iostream>
#include<thread>

#ifdef _WIN32
#include<windows.h>
#include<process.h>
#else
#include <pthread.h>
#endif


#ifdef _WIN32
unsigned int __stdcall print_block(void* arg)
#else
void*  print_block(void* arg)
#endif

{
    char c = '$';
    int n = 1000;
    for (int i = 0; i<n; ++i)
    {
        std::cout << c;
    }
    std::cout << '\n';
    #ifdef _WIN32
    return 0;
#else
    return;
#endif

}

void test_thread()
{
    ThreadVito  th1(print_block);
    th1.join();
}

int main()
{
    test_thread();
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值