无法使用for循环创建多个线程 [英] Unable to create multiple threads using for loop

查看:295
本文介绍了无法使用for循环创建多个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用for循环创建3个线程。以下是我的代码片段:



 DWORD WINAPI ThreadProc0(LPVOID param)
{
return 0;
}
DWORD WINAPI ThreadProc1(LPVOID参数)
{
返回0;
}
DWORD WINAPI ThreadProc2(LPVOID参数)
{
返回0;
}

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
DWORD threadId = 0;
int max_number = 1;
//开始线程
typedef DWORD(WINAPI * THREADPROCFN)(LPVOID lpParameter);
THREADPROCFN函数[3] = {ThreadProc0,ThreadProc1,ThreadProc2};
for(int i = 0; i< max_number; i ++)
{
CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)& function [i] ,
(LPVOID)i,
0,
NULL
);
}
}





代码编译成功但执行时错误是solution.exe已经停止工作。当我调试代码时,我收到以下错误:



0x0034fd00处的未处理异常Solution.exe:0xC0000005:访问冲突。



等待帮助。

解决方案

小错误:

Quote:

int max_number = 1;

这应该是 int max_number = 3; 对吗?





问题:

引用:

(LPTHREAD_START_ROUTINE)& function [i],

这应该是

(LPTHREAD_START_ROUTINE)函数[i],





(不含& operator)。


此代码可以正常工作并创建3个工作线程,请记住,您需要



返回值为int



int max_number = 3



function [i]



 DWORD WINAPI ThreadProc0(LPVOID param)
{
return 0 ;
}
DWORD WINAPI ThreadProc1(LPVOID参数)
{
return 0 ;
}
DWORD WINAPI ThreadProc2(LPVOID参数)
{
return 0 ;
}

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine ,
_In_ int nCmdShow)
{
DWORD threadId = 0 ;
int max_number = 3 ;
// 启动主题
typedef DWORD(WINAPI * THREADPROCFN)(LPVOID lpParameter);
THREADPROCFN函数[ 3 ] = {ThreadProc0,ThreadProc1,ThreadProc2};
for int i = 0 ; i< max_number; i ++)
{
CreateThread(NULL,
0
function [i ],
(LPVOID)i,
0
NULL
);
}


return 0 ;
}





输出:



线程0x2c4有退出代码0(0x0)。

线程0x1160退出代码0(0x0)。

线程0xc54退出代码0(0x0)。

程序''[3520] Win32Project1.exe'已退出,代码为0(0x0)。


代码正在编译,因为您正在编译不兼容的类型。所以编译器无法检查类型。删除不必要的 typedef 和转换,并传递正确的参数(函数数组的元素,而不是地址):

< pre lang =c ++> LPTHREAD_START_ROUTINE函数[ 3 ] = {ThreadProc0,ThreadProc1,ThreadProc2};
for int i = 0 ; i< max_number; i ++)
{
CreateThread(NULL,
0
function [i ],
(LPVOID)i,
0
NULL
);
}


I am trying to create 3 threads using a for loop. Following is my code snippet:

DWORD WINAPI ThreadProc0(LPVOID param)
{
   return 0; 
}
DWORD WINAPI ThreadProc1(LPVOID param)
{
    return 0;
}
DWORD WINAPI ThreadProc2(LPVOID param)
{
    return 0;
}

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
   DWORD threadId = 0;
   int max_number=1;
   //Start the threads
   typedef DWORD (WINAPI * THREADPROCFN)(LPVOID lpParameter);
   THREADPROCFN function[3] = {ThreadProc0,ThreadProc1,ThreadProc2} ;
   for (int i = 0; i < max_number; i++) 
   {
      CreateThread( NULL,
                    0,
                    (LPTHREAD_START_ROUTINE)&function[i],
                    (LPVOID) i,
                    0,
                    NULL
                   );
   }
}



The code is compiled successfully but when executed, the error is solution.exe has stopped working. When I Debug the code, I get the following error:

Unhandled exception at 0x0034fd00 in Solution.exe: 0xC0000005: Access violation.

Waiting for help.

解决方案

Minor error:

Quote:

int max_number=1;

This should be int max_number=3; right?


The problem:

Quote:

(LPTHREAD_START_ROUTINE)&function[i],

This should be

(LPTHREAD_START_ROUTINE)function[i],



(without & operator).


This code works without errors and creates 3 worker threads, keep in mind that you need a

return value as int and

int max_number = 3 and

function[i]:

DWORD WINAPI ThreadProc0(LPVOID param)
{
   return 0; 
}
DWORD WINAPI ThreadProc1(LPVOID param)
{
    return 0;
}
DWORD WINAPI ThreadProc2(LPVOID param)
{
    return 0;
}
 
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	DWORD threadId = 0;
	int max_number=3;
	//Start the threads
	typedef DWORD (WINAPI * THREADPROCFN)(LPVOID lpParameter);
	THREADPROCFN function[3] = {ThreadProc0,ThreadProc1,ThreadProc2} ;
	for (int i = 0; i < max_number; i++) 
	{
		CreateThread( NULL,
					0,
					function[i],
					(LPVOID) i,
					0,
					NULL
					);
	}

	
	return 0;
}



Output:

The thread 0x2c4 has exited with code 0 (0x0).
The thread 0x1160 has exited with code 0 (0x0).
The thread 0xc54 has exited with code 0 (0x0).
The program ''[3520] Win32Project1.exe'' has exited with code 0 (0x0).


The code is compiling because you are casting incompatible types. So the compiler can not check the types. Remove the unnecessary typedef and the casting, and pass the correct parameter (the element of the function array, not the address to it):

LPTHREAD_START_ROUTINE function[3] = {ThreadProc0,ThreadProc1,ThreadProc2} ;
for (int i = 0; i < max_number; i++)
{
   CreateThread( NULL,
                 0,
                 function[i],
                 (LPVOID) i,
                 0,
                 NULL
                );
}


这篇关于无法使用for循环创建多个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆