C++ 线程(一)

来源:互联网 发布:生产计划编制软件 编辑:程序博客网 时间:2024/06/06 01:01

#include <process.h>
#include <iostream>
#include <windows.h>

using std::cin;
using std::cout;
using std::endl;
typedef void *HANDLE;
class Thread
{
public:
        void start();
        virtual void run();
        HANDLE getThread();
private:
        HANDLE hThread;
        static void agent(void *p);
};

void Thread::start()
{

 cout << "hello world" <<endl;
       hThread =(HANDLE)_beginthread(agent, 0, (void *)this);
}
void Thread::run()
{
        cout << "Base Thread" << endl;
}
void Thread::agent(void *p)
{
        Thread *agt = (Thread *)p;
        agt->run();
}

HANDLE Thread::getThread()
{
        return hThread;
}

class DerivedThread: public Thread
{
public:
        void run();
};

void DerivedThread::run()
{
        cout << "Derived Thread" << endl;
}

int main(int argc, char *argv[])
{
        DerivedThread *dt = new DerivedThread();
        dt->start();
        WaitForSingleObject(dt->getThread(), INFINITE);
}

 

编译器设置步骤如下: 工程 → 属性 →
C/C++
→ 分类:Code  Generation
Use run-time library
  debug
下选择
debug multithreaded
 
release
下选择
multithreaded。

0 0
原创粉丝点击