Win32: How can I make two threads communicate each other?

thebluetropics 1,046 Reputation points
2022-09-25T08:34:06.643+00:00

main.cpp:

   DWORD WINAPI ThreadMain(LPVOID lpParam) {  
       // doing thread-specific tasks, and send results to main thread at anytime  
       return 0;  
   }  
     
   int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {  
       DWORD threadId;  
       HANDLE hThread;  
     
       CreateThread(/* ... */);  
     
       // doing main tasks here, and also looking for `hThread` is send an event  
     
       WaitForSingleObject(hThread, INFINITE);  
   }  

In short, I need a way for two threads communicate each other.

Windows development | Windows API - Win32
Developer technologies | C++
{count} vote

3 answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2022-09-25T12:55:34.187+00:00

    You can see MSDN doc and sample at : Sharing common resources between threads

    0 comments No comments

  2. Guido Franzke 2,191 Reputation points
    2022-09-26T06:46:01.5+00:00

    Hello,
    another way is by sending messages. When you create the thread, put the main window handle into the thread's argument, and save the thread variable. Then you can for example use SendMessage.
    Regards, Guido

    0 comments No comments

  3. Tong Xu - MSFT 2,546 Reputation points Microsoft External Staff
    2022-09-26T10:34:37.283+00:00

    Hi!
    Welcome to Microsoft Q&A!
    I give you two methods to implement thread communication and thread synchronization. Both methods implement the same functionality .
    First, we can use Event, here is the sample:

     #include<windows.h>  
        #include<cstdio>  
        HANDLE g_hSet, g_hClear;  
        int g_Max = 10;//the limit of goods  
        int g_Number = 0;  
    //producer  
        DWORD WINAPI ThreadProduct(LPVOID pM)  
        {  
         for (int i = 0; i < g_Max; i++)  
         {  
         WaitForSingleObject(g_hSet, INFINITE);  
         g_Number = 0;  
         DWORD id = GetCurrentThreadId();  
         printf("Producer %d put stuff in %d market\n", id, g_Number);  
                SetEvent(g_hClear);  
         }  
         return 0;  
        }  
    //Consumer  
        DWORD WINAPI ThreadConsumer(LPVOID pM)  
        {  
            for (int i = 0; i < g_Max; i++)  
            {      
                WaitForSingleObject(g_hClear, INFINITE);   
                    g_Number = 0;  
                    DWORD id = GetCurrentThreadId();  
                    printf("-----Consumer %d take out stuff in %d market\n", id, g_Number);  
                    SetEvent(g_hSet);  
            }  
            return 0;  
        }  
        int main(int argc, char* argv[])  
        {  
            g_hClear = CreateEvent(NULL, FALSE, FALSE, NULL);  
            g_hSet = CreateEvent(NULL, FALSE, TRUE, NULL);  
            HANDLE hThread[2];  
            hThread[0] = ::CreateThread(NULL, 0, ThreadProduct, NULL, 0, NULL);  
            hThread[1] = ::CreateThread(NULL, 0, ThreadConsumer, NULL, 0, NULL);  
          
            WaitForMultipleObjects(2, hThread, TRUE, INFINITE);  
            CloseHandle(hThread[0]);  
            CloseHandle(hThread[1]);  
          
             
            CloseHandle(g_hSet);  
            CloseHandle(g_hClear);  
          
            return 0;  
          
        }  
    

    ===================

    Then we can use mutex:

        #include<cstdio>  
        #include<windows.h>  
        HANDLE hMutex;  
        int g_Max = 10;  
        int g_Number = 0;  
          
        DWORD WINAPI ThreadProduct(LPVOID pM)  
        {    
            for (int i = 0; i < g_Max; i++)  
            {   
            
                WaitForSingleObject(hMutex, INFINITE);//  
                if (g_Number == 0)  
                {  
                    g_Number = 1;  
                    DWORD id = GetCurrentThreadId();  
                    printf("Producer%dput stuff in%dmarket\n", id, g_Number);  
                      
                }  
                else   
                {  
                    printf("-------------------------\n");  
                    i--;  
                }  
                ReleaseMutex(hMutex);  
            }  
            return 0;  
        }  
          
        DWORD WINAPI ThreadConsumer(LPVOID pM)  
        {  
            for (int i = 0; i < g_Max; i++)  
            {  
                WaitForSingleObject(hMutex, INFINITE);  
                if (g_Number == 1)  
                {  
                    g_Number = 0;  
                    DWORD id = GetCurrentThreadId( );  
                    printf("-----Consumer%dtake out stuff in%dmarket\n", id, g_Number);  
                      
                }  
                else  
                {  
                    printf("+++++++++++++++++++++\n");  
                    i--;  
                }  
                ReleaseMutex(hMutex);  
            }  
            return 0;  
        }  
        int main(int argc ,char*argv[])  
        {  
            hMutex = CreateMutex(NULL, FALSE, NULL);  
            HANDLE hThread[2];  
            hThread[0] = ::CreateThread(NULL, 0, ThreadProduct, NULL, 0, NULL);  
            hThread[1] = ::CreateThread(NULL, 0, ThreadConsumer, NULL, 0, NULL);  
              
            WaitForMultipleObjects(2, hThread, TRUE, INFINITE);  
            CloseHandle(hThread[0]);  
            CloseHandle(hThread[1]);  
          
              
            CloseHandle(hMutex);  
              
            return 0;  
    }  
    

    ===================

    If you have problems with the code, you can ask me anytime.
    Thank you.
    Tong


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.