Multi-thread synchronization event control method between processes

Source: Internet
Author: User

There are many. Below is a message sent by the sender to notify the receiver. After receiving the message from the sender, the receiver can perform read and write operations on the memory. In the program design, a Windows registered message is used for message transmission. First, the message registration is performed during the initialization process of the sending process: m_nmsgmapped =: registerwindowsmessage ("mapped"); m_nmsghandle = :: registerwindowsmessage ("handle"); m_nmsgshared =: registerwindowsmessage ("shared"); send a message to the receiving process: cwnd * pwndrecv = findwindow (lpclassname, "receive "); pwndrecv-> sendmessage (m_msgmapped, 0, 0); pwndrecv-> sendmessage (m_nmsghandle, (uint) getcurrentprocessid (), (long) PAPP-> m_hglobalhandle); pwndrecv-> sendme Ssage (m_nmsgshared, 0, 0); you can send the wm_copydata message as follows: staticcopydatastructcds; // The user stores the data pwnd-> sendmessage (wm_copydata, null, (long) & CDs ); the receiver process must also register the message for initialization: unitcrecvapp: m_nmsgmapped =: registerwindowsmessage ed ("mapped"); unitcrecvapp: m_nmsghandle =: registerwindowsmessage ("handle"); unitcrecvapp :: m_nmsgshared =: registerwindowsmessage ("shared"); the message ing function is as follows: on_registered_massage (crecvapp: m_nmsgmapped, onregmsgmapped) Functions (crecvapp: m_nmsghandle, onregmsghandle) on_registered_massage (crecvapp: m_nmsgshared, onregmsgshared) can use the above technology to read and write data in the receiving process.

 

------------------------------------------------------

 

Multi-thread synchronization event control method between processes

Because the event object is valid within the system, another process (that is, an application, also
Is a thread) can call the openevent function to obtain the object handle through the object name, but the object
It must have been created and can be used for resetevent, setevent, and
Waitformultipleobjects and other functions. In this way, the thread of one process can control another process.
Run the generated thread. The following statement obtains the process above through the object name "myevent"
The generated hnoblockevent object handle, which is used to set the object state to nonsignaled.
In the above loopfunc function, the above thread is paused because the object state has changed.

Handle hevent = openevent (event_all_access, true, "myevent ");

Resetevent (hevent );

The first parameter of the openevent function indicates the access permission of the call thread of the function to the event object. For example:
Let the thread have all the access permissions of the object, select the event_all_access parameter, so that the thread can use
The resetevent function changes the object state. The true parameter indicates that the child process derived from this process can
The last parameter indicates the name of the event object. Use the following statement to set the object
If the hnoblockevent status is signaled, the thread can continue to run, for example, setevent (hevent ).

When the process no longer uses this handle, it can use the closehandle function to close the object handle, but for the same event
Object, because it may still be used in other threads, only all referenced sentences
The object will be released by the system only after the handle is closed. All event objects mentioned in this article are between the main thread and thread.
As well as the control role between different processes 1 is shown in:
① ── ─ ── ①: Close the window
── ─ ┤ Hcloseevent ── ┐ ②: Response to the above event
│ └ ── ─ ┘ │ |
│ ── ─ ┐ ↓ | Pause/resume thread running
│ Hthread or │ ② ┌ ── ┐ ── ─ ┐ ┌ ── ┐
─ ── ┐ ┌ ┤ Hnocloseevent when there are two threads waiting for hnoblockevent when there are two processes 2 │
│ The main thread is always starting and running ── ─
│/Process 1 → ┐ ── ─ ┐ │ | between different processes
└ ── ┬ ── ⑴ (1) └ ┤ hwatchevent ── ┘ │ | Address limit
↑ ── ─ ┘ │
│ ┌ ── ─ ┐ │ (1): Monitoring Data
── ┤ Hntyevent ├ ← ── ⑵ (2): The thread meets the monitoring conditions.
└ ── ─ ┘ (2)

-----------------------------------------------------------------

4. send and receive messages between processes

In practical applications, processes need to send and receive Windows messages to notify them to communicate with each other. The sender sends a message to notify the recipient, after receiving the message from the sender, the receiver can perform read/write operations on the memory.

In the program design, we use Windows to register a message for message transmission. First, we register the message during the initialization process of the sending process:
M_nmsgmapped =: registerwindowsmessage ("mapped ");
M_nmsghandle =: registerwindowsmessage ("handle ");
M_nmsgshared =: registerwindowsmessage ("shared ");
Send a message to the receiving process while the program is running:
Cwnd * pwndrecv = findwindow (lpclassname, "receive ");
Pwndrecv-> sendmessage (m_msgmapped, 0, 0 );
Pwndrecv-> sendmessage (m_nmsghandle,
(Uint) getcurrentprocessid (), (long) PAPP-> m_hglobalhandle );
Pwndrecv-> sendmessage (m_nmsgshared, 0, 0 );
You can send the wm_copydata message as follows:
Static copydatastruct CDs; // data stored by the user
Pwnd-> sendmessage (wm_copydata, null, (long) & CDs );

Message registration is also required for recipient process initialization:

Unit crecvapp: m_nmsgmapped =: registerwindowsmessage ("mapped ");
Unit crecvapp: m_nmsghandle =: registerwindowsmessage ("handle ");
Unit crecvapp: m_nmsgshared =: registerwindowsmessage ("shared ");
The message ing function is as follows:
On_registered_massage (crecvapp: m_nmsgmapped, onregmsgmapped)
On_registered_massage (crecvapp: m_nmsghandle, onregmsghandle)
On_registered_massage (crecvapp: m_nmsgshared, onregmsgshared)
In these message functions, we can use the above technology to read and write data in the receiving process.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.