Communication threads and processes of synchronization mode

1, communication between threads

Use of global variables

Mainly due to the multiple threads may change global variables, global variables declared volatile best

Communicate using messages
in Windows programming, each thread can have its own message queue (UI thread that comes with the default message queue and message loop, worker threads need to manually implement the message loop), so you can use a message for inter-thread communication sendMessage , postMessage.
1) the definition of WM_USER + = WM_THREAD_SENDMSG message 20 is #define;
2) add a function declaration message int OnTSendmsg afx_msg ();
. 3) add a message map ON_MESSAGE (WM_THREAD_SENDMSG, OnTSM)
implementing a function 4) was added OnTSM (); and
5) the function thread Post a message add PostMessage function

CEvent implemented using an event-based inter-thread communication
Event object has two states: the signal and no signal, the thread can monitor events in the signal state, to perform the operations of the event at the appropriate time.
1) Create an object class CEvent: CEvent threadStart; it is not in the default state of the communication;
2) threadStart.SetEvent (); it is in a communication state;
3) calls the WaitForSingleObject () to monitor target CEvent

2, synchronous way between threads

Each thread can access the public variables in the process, need to pay attention to the problem of resources, the use of multi-threaded process is how to prevent two or more threads simultaneously access the same data, so as not to undermine the integrity of the data.

Interactions among data comprising:
a direct relationship constraints, i.e., the processing result of a thread, another thread is input, thus directly restricts between the threads, this relationship can be referred to as synchronization relationship
2, indirect relationship constraints that two threads need to access the same resource, the resource is accessed by only one thread at the same time, this relationship is called inter-thread mutually exclusive access to resources, in a sense mutually exclusive relationship is a constraint more small synchronization

Synchronization between threads, there are four ways

Critical section

Critical region corresponds to a CcriticalSection object when a thread needs to access protected data, call EnterCriticalSection function; after the completion of operations on data protection, call LeaveCriticalSection function to release ownership of the critical section object so that another thread can capture the critical region object and access to protected data.
PS: The key target segment will record the thread handle owns the object that it has a "thread ownership" concept, that is, into threaded code section before the leave, can be repeated to enter the critical region. So the key segments can be used for mutual exclusion between threads, but can not be used for synchronization (sync need to enter in a thread in another thread leave)

Mutex

Mutex and critical section is very similar, but relatively complex when using some (mutex kernel objects), not only can achieve synchronization between threads of the same application, you can also synchronize between different processes in order to achieve security resources shared.
PS: 1, because the mutex ownership also have the concept of threads, it is also only resources between threads exclusive access, not because the thread synchronization;
2, due to the mutex is a kernel object, so it can be inter-process communication, It also has a nice feature, that is, when inter-process communication perfect solution to the "abandonment" issues

signal

Semaphore usage and exclusive usage is very similar, except that it may be the same time allows multiple threads access the same resource, PV operation
PS: the perfect event can solve the synchronization problem between threads, semaphores while also belong to the kernel objects, It can be used for communication between processes

event

Events set into manual and automatic event set event. It contains an internal event Event usage count (all cores are objects), a Boolean value indicating that an event set manually or automatically set event, another event or absence Boolean value that triggers. Triggered by SetEvent (), of the ResetEvent () is set to be triggered.
PS: the event is a kernel object that can resolve inter-thread synchronization problem, so the problem can be resolved mutually exclusive

3, inter-process communication

Interprocess communication, also known as the IPC (Inter-Process Communication), refers to the mutual communication between a plurality of processes, the method of exchanging information.

Depending upon the amount of information communication process, the communication process can be divided into two types:

1, low level communication, the communication control information (mainly for transmitting synchronization, mutex, and suspended between termination process control information, etc.)

2, advanced communications, a large number of the communication data information (mainly for the exchange and sharing of data between processes data blocks, a common pipe has advanced communication, message queues, shared memory, etc.).

details as follows:

Conduit (pipe): the pipe is half-duplex communication mode, data flows only one way, but can only be used in a process having a genetic relationship between. Kinship process usually refers to the process of parent-child relationship.
Named pipe (named pipe): named pipe is half-duplex communication, but allows no communication between the kinship process.
Semaphore (semophore): semaphore is a counter, multiple processes can be used to control access to shared resources. Not for bulk data exchange, and for synchronization. Between multiple threads is often used as a locking mechanism to prevent other processes is also a process to access the resource when accessing resources. Therefore, the main as well as inter-process synchronization means between the different threads within the same process.
Message queue (message queue): message queue is a linked list of messages, by message queue identifier stored in kernel. Signaling message queue overcome the less information, only the carrier pipe plain byte stream buffer size is limited, and other shortcomings.
Signal (signal): Signal is a more sophisticated means of communication, an event used to notify the receiving process has occurred.
[Shared memory (shared memory)]: Shared memory is memory mapped a period that can be accessed by other processes, this shared memory created by a process, but can be accessed by multiple processes. IPC shared memory is the fastest way, it is for the other inter-process communication running low efficiency specifically designed. It is often associated with other communication mechanisms, such as two signals, used in conjunction to achieve synchronization and communication between processes.
Socket (socket): Solutions sleeve port is an inter-process communication mechanism, with various other communication mechanism is that it can be used for communication between processes on different machines.

Published 254 original articles · won praise 23 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_30242987/article/details/104734180