在类中使用类成员函数创建线程

来源:互联网 发布:数据库设计第三范式 编辑:程序博客网 时间:2024/06/07 08:17

demo书写大概步骤:

     1》创建一个类,在类中创建一个函数

     2》在构造函数中创建线程,使用类中的成员函数

类中代码:

 类头:

#pragma once#include <windows.h>class tt{public:tt(void);~tt(void);private:static DWORD WINAPI ThreadProc(LPVOID lpThreadParameter);void k();};


类体:

#include "stdafx.h"#include "tt.h"tt::tt(void){printf("create tt\n");CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,this,0,NULL); //关键位置}tt::~tt(void){}DWORD WINAPI tt::ThreadProc(LPVOID lpThreadParameter){((tt*)lpThreadParameter)->k();return TRUE;}void tt::k(){while (true){printf("hello world!\n");Sleep(1000);}}


主函数代码:

// gxbTest.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <windows.h>#include "tt.h"int _tmain(int argc, _TCHAR* argv[]){tt t;getchar();return 0;}

实验效果:



0 0