Win32编程下使用线程

最近在进行win32的程序开发,win32下提供CreateThread创建线程,线程通过一个回调函数执行,而类的成员函数不能直接作为回调函数,需要通过类的静态函数作为中转才能访问到类的属性,用起来很不方便,与Qt相比之下,发现Qt封装得好用很多

后面在网上找到了一个别人封装的使用线程的方式,使用和Qt里继承QThread的实现类似,这里做个记录

代码如下

#ifdef _AFXDLL
#include <afx.h>
#else
#include <windows.h>
#endif

#include <process.h>    // _beginthread(), _endthread()
#include <iostream>

class cThread
{
public:
  cThread  () : threadid_ (-1) {}
  ~cThread () { if (threadid_ != -1) stop();}

 int threadid () const { return threadid_;}

  bool start ()
  {
    threadid_ = _beginthreadex (0, 0, thread_, this,
                                CREATE_SUSPENDED, (unsigned int*) &threadid_);
    if (threadid_ != 0)
      ResumeThread ((HANDLE)threadid_);
    return (threadid_ != 0);
  }
  // Start the thread running

  bool stop  ()
  {
    TerminateThread ((HANDLE) threadid_, -1);
    return true;
  }
  // Stop the thread. Ungraceful and may result in locking/resource problems.

  bool wait (unsigned int seconds = 0)
  {
    DWORD wait = seconds * 1000;
    if (wait == 0) wait = INFINITE;
    DWORD status = WaitForSingleObject ((HANDLE) threadid_, wait);
    return (status != WAIT_TIMEOUT);
  }
  // Wait for thread to complete

  void sleep (unsigned int msec) { Sleep (msec);}
  // Sleep for the specified amount of time.

protected:
  int threadid_;

  static unsigned int __stdcall thread_ (void* obj)
  {
    // Call the overriden thread function
    cThread* t = reinterpret_cast<cThread*>(obj);
    t->thread ();
    return 0;
  }

  virtual void thread () {
    _endthreadex (0);
    CloseHandle ((HANDLE) threadid_);
  }
  // Thread function, Override this in derived classes.
};

通过_beginthreadex进行封装,据说_beginthreadex内部也是调用CreateThread,至于这两个的联系和区别网上有很多,可自行查看。

这里是封装成一个类,通过继承该类,重载thread函数,线程的执行内容放在thread函数中,通过start开启线程,这和Qt中继承QThread,重载run函数是一样的

class CTest : public cThread
{

public:
    void thread	();

};

void CTest::thread()
{
    for(int i=0;i<10;i++)
    {
        printf("thread\n");
        Sleep(1000);
    }
    cThread::thread();
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    CTest test;
    test.start();

    return a.exec();
}

如果要调用类的成员函数,只要在CTest拿到该类的对象就可以直接调用,其他类还可以通过回调的方式取到执行结果

class CTest : public cThread
{

public:
    void thread	();

    void bindFun(const std::function<void ()> &fun) {
        fun1 = fun;
    }

private:
    std::function<void ()> fun1;

};

void CTest::thread()
{
    for(int i=0;i<10;i++)
    {
        printf("thread\n");
        if(i%2 == 0)
            fun1();
        Sleep(1000);
    }
    cThread::thread();
}

class Test2
{
public:
    Test2(){}


    void add()
    {
        ++count;
        printf("count %d\n", count);
    }

private:
    int count = 0;

};


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    CTest test;
    Test2 test2;

    std::function<void ()> funtional = std::bind(&Test2::add, &test2);
    test.bindFun(funtional);
    test.start();

    return a.exec();
}

通过std::functionstd::bind实现回调,例子中线程执行偶数触发Test2的add函数

执行结果

简单理解就是CTest的线程执行结果发送到外部,相当于Qt中信号槽,将线程执行结果通过信号槽的方式供外部获取。当线程用于检测状态时,这个方式就很好用了,外部就能够响应检测的状态

封装的这个线程用户,用起来就很方便,适用于多场景使用,利于扩展

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值