简体   繁体   English

在C,多线程,多窗口调用一个窗口过程,每个调用将使用新的局部变量或我需要一个互斥?

[英]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.

相关问题 我是否需要为多线程 C 代码编写显式内存屏障? - Do I need to write explicit memory barrier for multithreaded C code? 我是否必须在多线程服务器中使用互斥量来保护bufferevent_write - Do I have to protect bufferevent_write with mutex in a multithreaded server 我需要释放局部变量吗? - Do I need to free local variables? 如果我有多个for循环,我可以每次使用我还是必须使用不同的变量? - If i have multiple for loops can i use i each time or do i have to use different variables? 在这种情况下我真的需要互斥锁吗? - Do I really need mutex lock in this case? 为每个结构使用多个互斥锁的最佳方法? (例如 2 个结构由 4 个线程操作) - best way to use multiple mutex one for each structure ? (2 structures being manipulated by 4 threads for example) (C)在多线程客户端和服务器中使用互斥锁 - (C) Using mutex in multithreaded client and server 多线程C程序中的信号量互斥并发问题 - Semaphore Mutex Concurrency Issue in multithreaded C program 为什么需要在多线程程序中使用Pthread_sigmask来确保set处理程序正在处理信号? - Why do I need to use Pthread_sigmask in multithreaded programs to assure that a signal is being handled by the set handler? 需要在C中将全局变量转换为局部变量 - Need to convert global variables into local variables in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM