zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++创建线程_C语言网络编程创建线程

2023-06-13 09:11:08 时间

在window系统中编写控制台程序,创建线程

使用CreateThread()函数创建,则线程函数必须申明为DWORD WINAPI;

使用_beginthreadex()创建,则线程函数必须申明为unsigned int WINAPI;

并需要设置环境:工程->设置->C/C++->Code Generation->Use run-time libray->选 Debug Multithread(多线程),或 Multithread.

例如:

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

DWROD WINAPI myfun1(void *pvoid)

{

cout<<“CreateThread”<<endl;

return 0;

}

unsigned int WINAPI myfun2(void *pvoid)

{

cout<<“_beginthreadex<endl;

return 0;

}

int main()

{

CreateThread(NULL,NULL, myfun1,NULL,NULL);

_beginthreadex(NULL,NULL,myfun2,NULL,NULL);

return 0;

}

将类成员函数作为线程函数方式:

1.将类成员申明为STATIC成员函数;

2.将函数申明为类的友元函数;

例如:

class MyTest

{

public:

static unsigned int WINAPI mythread(void *pvoid);

friend unsigned int WINAPI myFun(void *pvoid);

};

上一篇:TCHAR,CHAR,LPSTR,LPCSTR,char这几个数据类型有何不同

下一篇:全局变量、局部变量、静态全局变量、静态局部变量区别 操作系统编译器怎么区别

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/179715.html原文链接:https://javaforall.cn