您的位置:首页 > 编程语言 > C语言/C++

C++ 线程(一)

2015-07-24 13:50 417 查看
#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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: