[英]In C, multithreaded, multiple windows calling one Window Procedure, will each call use new local variables or do I need a mutex?
I tried to explain it all in the title: I have a multithreaded C program which will have multiple windows calling one Window Procedure. 我试图在标题中解释它:我有一个多线程的C程序,它将有多个窗口调用一个Window Procedure。
There is some processing done in the window procedure. 在窗口过程中完成了一些处理。 Do I need to protect it, or will each call to the window procedure be separated in memory? 我需要保护它,还是每次调用窗口过程都要在内存中分开?
My instinct is that I don't need a mutex, because they're all local variables, is this wrong? 我的直觉是我不需要互斥锁,因为它们都是局部变量,这是错误的吗?
LRESULT APIENTRY EditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_GETDLGCODE)
return DLGC_WANTALLKEYS;
else if(uMsg == WM_CHAR)
{
if( (int) wParam == 13)
{
char* strCurrentCommand;
unsigned long ulThisConversation = GetConversation(0, 0, hwnd, 0, 0);
...
I'm concerned with the local variables strCurrentCommand and ulThisConversation. 我关注局部变量strCurrentCommand和ulThisConversation。
Local variables and parameters to functions go on the stack. 函数的局部变量和参数在堆栈中。 Each thread gets its own stack, and each invocation of a function gets a space on the stack of the thread it's running in for its parameters and local variables. 每个线程都有自己的堆栈,每次调用函数都会在其运行的线程的堆栈中获取一个空间,用于其参数和局部变量。 So you're fine. 所以你没事。
您不需要使用互斥锁保护堆栈变量。
No need to protect local variables, as each call to the window proc function will allocate new copies on the stack. 无需保护局部变量,因为每次调用window proc函数都会在堆栈上分配新副本。
But keep in mind that to avoid the need for a mutex or other synchronization mechanisms you also need to make sure any functions called from inside your window proc function (like GetConversation
and any functions this function calls) are thread safe. 但请记住,为了避免使用互斥锁或其他同步机制,您还需要确保从窗口proc函数内部调用的任何函数(如GetConversation
和此函数调用的任何函数)都是线程安全的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.