beginthread_使用参数调用同一个类中的函数 [英] beginthread_ call a function in the same class with parameters

查看:432
本文介绍了beginthread_使用参数调用同一个类中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ThreadHandleClientThreadFunction中添加(void *)clientSocket作为参数?





main.h

 class Networker 
{
public:
Networker(u_short port);
~Networker();

...
void ServerThreadFunction();
void HandleClientThreadFunction(void * parameters);

static void __cdecl ThreadHandleClientThreadFunction(void * pThis)
{
static_cast< Networker *>(pThis) - > HandleClientThreadFunction(); //错误在这里
};

static void __cdecl ThreadServerThreadFunction(void * pThis)
{
static_cast< Networker *>(pThis) - > ServerThreadFunction();
};





Main.cpp



  void  Networker :: HandleClientThreadFunction( void  * parameters)
{
// 输入参数是客户端套接字
SOCKET clientSocket =(SOCKET)参数;

}

void Networker :: ServerThreadFunction()
{
... 。
for (;;)
{
// < span class =code-comment>等待新客户端
clientSocket = accept(listenSocket,NULL,NULL);

// 检查新客户是否有效
if (clientSocket == INVALID_SOCKET)
{
closesocket(listenSocket);
break ;
}

// 启动一个新线程来处理新客户端
_beginthread(ThreadHandleClientThreadFunction, 0 ,( void *)clientSocket);
}
}

解决方案

您需要的技术在很多地方都有详细解释。例如:

http://stackoverflow.com/questions/5968076/ pass-parameters-to-beginthreadex [ ^ ]。



想法是:线程入口点地址旁边的参数是用于将任何数据传递给此入口点函数的void指针;所以,你通过这个指针传递你的数据的地址,这个数据将传递给你的函数;在新创建的线程中调用此函数时,您需要将此void指针放回到指向数据结构的指针。你会在上面提到的答案中找到代码示例。



-SA


为什么不穿你改变了:

 _beginthread(ThreadHandleClientThreadFunction,0,(void *)clientSocket); 



to:

 _beginthread(ThreadHandleClientThreadFunction,0,(void *)this); 





现在,这个:

 static_cast< networker *>(pThis) - > HandleClientThreadFunction(); // ERROR HERE 



甚至不应该编译,因为HandleClientThreadFunction被声明为:

 void HandleClientThreadFunction (void * parameters); 





祝你好运

Espen Harlinn


< blockquote>你好。我认为您需要传递指向Networker实例的指针,而不是通过您的线程函数对指针执行的操作来判断clientSocket。

此外,线程函数中的强制转换应为 reinterpret_cast 。我不认为静态强制转换会将其剪切为将 void * 转回到类实例指针中,除非他们已经改变了我在C ++ 11中的规则。

可能你的 clientSocket 也需要是<$ c $的成员c> Networker 而不是局部变量,但我不能不看完整个代码。


How to add (void*)clientSocket as a parameter in ThreadHandleClientThreadFunction ?


main.h

class Networker
{
public :
	Networker(u_short port);
	~Networker();

...
	void ServerThreadFunction();
	void HandleClientThreadFunction(void* parameters);

	static void __cdecl ThreadHandleClientThreadFunction(void * pThis)
  {
	  static_cast<Networker*>(pThis)->HandleClientThreadFunction();  //ERROR HERE
  };

	static void __cdecl ThreadServerThreadFunction(void * pThis)
  {
	  static_cast<Networker*>(pThis)->ServerThreadFunction();  
  };



Main.cpp

void Networker::HandleClientThreadFunction(void *parameters) 
{
	// Input parameter is the client socket
	SOCKET clientSocket = (SOCKET)parameters;

}

void Networker::ServerThreadFunction()
{
....
for (;;)
{
		// Wait for a new client
		clientSocket = accept(listenSocket, NULL, NULL);

		// Check if new client is valid
		if(clientSocket == INVALID_SOCKET)
		{
			closesocket(listenSocket);
			break;
		}

		// Start a new thread to handle the new client
		_beginthread(ThreadHandleClientThreadFunction, 0, (void*)clientSocket);
}
}

解决方案

The technique you need is explained in detail in many places. See for example:
http://stackoverflow.com/questions/5968076/passing-parameters-to-beginthreadex[^].

The idea is: the parameter next to your thread entry point address is the void pointer used to pass any data to this entry point function; so, you pass the address of your data via this pointer, and this data will be passed to your function; when this function is called in your newly created thread, you will need to case this void pointer back to the pointer to your data structure. You will find the code samples in the answers referenced above.

—SA


Why don''t you change:

_beginthread(ThreadHandleClientThreadFunction, 0, (void*)clientSocket);


to:

_beginthread(ThreadHandleClientThreadFunction, 0, (void*)this);



Now, this:

static_cast<networker*>(pThis)->HandleClientThreadFunction();  //ERROR HERE


should not even compile, as HandleClientThreadFunction is declared as:

void HandleClientThreadFunction(void* parameters);



Best regards
Espen Harlinn


Hello again. I think you need to be passing a pointer to the Networker instance rather than the clientSocket judging by what your thread function does with the pointer.
Also the cast in the thread function should be a reinterpret_cast. I don''t think a static cast will cut it to turn a void* back into a class instance pointer unless they''ve changed the rules on me in C++11.
Possibly also your clientSocket needs to be a member of Networker rather than a local variable but I can''t really say without seeing the whole code.


这篇关于beginthread_使用参数调用同一个类中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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