C++创建线程

在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这几个数据类型有何不同

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值