Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of communication between threads

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the methods of inter-thread communication, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

The methods of communication between threads are: 1, global variables, memory sharing between threads in the process, which is a common way of communication and interaction; 2, Message message mechanism; 3. CEvent object, CEvent is an object in MFC, which can change the trigger state of CEvent to realize the communication between threads.

There are three main methods of multithreaded communication:

1. Global variable

Memory sharing between threads in a process, which is a common way of communication and interaction.

Note: it is best to use volatile when defining a global variable in case the compiler optimizes it.

2.Message message mechanism

There are two common interfaces for Message communication: PostMessage and PostThreadMessage

PostMessage sends messages to the main window for the thread. PostThreadMessage is the communication interface between any two threads.

2.1.PostMessage ()

Function prototype:

B00L PostMessage (HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)

Parameters:

HWnd: the handle of the window whose window program receives messages. It is desirable to have two values with a specific meaning:

HWND.BROADCAST: messages are sent to all top-level windows of the system, including invalid or invisible windows that are not owned by themselves and overwritten

And pop-up windows. The message is not sent to the child window.

NULL: this function operates the same as the identifier PostThreadMessage function that calls the current thread with the parameter dwThread set to the current thread.

Msg: specifies the message to be sent.

WParam: specifies additional message-specific information.

IParam: specifies additional message-specific information.

Return value: if the function call is successful, a non-zero value is returned; if the function call fails, the return value is zero.

MS also provides a SendMessage method for inter-message communication, SendMessage (). The difference between him and PostMessage is:

SendMessage is synchronous, while PostMessage is asynchronous. SendMessage must wait for the sent message to be executed before returning.

2.2.PostThreadMessage ()

The PostThreadMessage method can send a message to a specified thread.

Function prototype: BOOL PostThreadMessage (DWORD idThread,UINT Msg,WPARAM wParam, LPARAM lParam)

Except for ThreadId, the parameters are basically the same as PostMessage.

The target thread accepts the message through the GetMessage () method.

Note: when using this method, the target thread must already have its own message queue. Otherwise, an ERROR_INVALID_THREAD_ID error will be returned. Can be used

PeekMessage () creates a message queue for the thread.

3.CEvent object

CEvent is an object in MFC, which can realize communication and synchronization between threads by changing the trigger state of CEvent.

After reading the above, do you have any further understanding of the methods of communication between threads? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report