C++线程问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++线程问题相关的知识,希望对你有一定的参考价值。

为什么编译下面的代码会出现这样的错误:D:\C++\操作系统\Memery.cpp(13) : error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (void)' to 'unsigned int (__stdcall *)(void *)'

#include<windows.h>
#include<iostream.h>
#include<stdlib.h>
#include "process.h"
//using namespace std;
void fun1(void);
void fun2(void);
HANDLE thread[2];
void main()

_beginthreadex(NULL,0,fun1,NULL,0,&thread[0]);
_beginthreadex(NULL,0,fun1,NULL,0,&thread[1]);

unsigned _stdcall fun1(void *)

cout<<"fun1"<<endl;

unsigned _stdcall fun2(void *)

cout<<"fun2"<<endl;

回调函数怎么写的?另外 你的线程ID应该定义成UINT类型,而非句柄。
uintptr_t _beginthreadex(
void *security,
unsigned stack_size,
unsigned ( *start_address )( void * ),
void *arglist,
unsigned initflag,
unsigned *thrdaddr
);

另外工程设置要设置成Multithread
参考技术A void fun1(void); 跟void fun1(void *); 是有区别的

前提指的是函数不带参数,后者指的是函数有一个参数,其类型是void *型指针
参考技术B 回调函数不是这样的格式,应该是DWORD WINAPI fun1(LPVOID pParam)

以上是关于C++线程问题的主要内容,如果未能解决你的问题,请参考以下文章

C++:线程池比单线程慢?

C++多线程问题

C++线程问题

多核 C++ 线程

C++多线程怎么实现

C++多线程阻塞主线程