Multi-threaded communication methods

Source: Internet
Author: User
Tags message queue posix

Transfer from http://www.cnblogs.com/mengyan/archive/2012/08/30/2664607.html

I. Process communication methods

It is necessary to describe the communication of the process before the description of the thread communication;

There are several ways to communicate between processes:

(1) Pipes: Pipelines can be used to communicate between affinity processes, allowing one process and another to communicate with a process that has a common ancestor to it.
(2) Named pipe (named pipe): The named pipe overcomes the limitation of the pipe without name, so that, in addition to having the functionality that the pipeline has, it allows for inter-process communication without affinity. Named pipes have corresponding file names in the file system. Named pipes are created by command Mkfifo or by system call Mkfifo.
(3) signal (Signal): signal is a more complex mode of communication, used to inform the receiving process of an event occurred, in addition to inter-process communication, the process can also send signals to the process itself; Linux in addition to supporting the UNIX early signal semantic function Sigal outside, It also supports the signal function sigaction that is semantically compliant with the POSIX.1 standard (in fact, the function is based on BSD, BSD in order to achieve a reliable signal mechanism, but also can unify the external interface, with the Sigaction function to re-implement the signal function).
(4) Message queue: Message Queuing is a linked table of messages, including POSIX Message Queuing system V Message Queuing. A process with sufficient permissions can add messages to the queue, and a process that is given Read permission can read the messages in the queue. Message queue overcomes the lack of signal carrying information, the pipeline can only carry the unformatted byte stream and the buffer size is limited.
(5) Shared memory: Allows multiple processes to access the same piece of memory space, is the fastest available IPC form. is designed for inefficient operation of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes.
(6) Memory mapping (mapped memories): Memory mapping allows any number of interprocess communication, and each process that uses the mechanism implements it by mapping a shared file to its own process address space.
(7) Semaphore (semaphore): primarily as a means of synchronization between processes and between different threads of the same process.
(8) Socket: A more general inter-process communication mechanism that can be used for inter-process communication between different machines. Originally developed by the BSD branch of the UNIX system, it can now be ported to other Unix-like systems: both Linux and System V variants support sockets.

Second, the method of thread communication

There are two main methods of thread communication:

(1) Shared memory

The method of sharing memory is described in the preceding ("producer consumer" related places); Here is another form:

Implementing shared variables for threads through an inner class

/** * Implement shared variables for threads through internal classes * */
public class Innersharethread {public static void Main (string[] args) { Mythread Mythread = new Mythread (); Mythread.getthread (). Start (); Mythread.getthread (). Start (); Mythread.getthread (). Start (); Mythread.getthread (). Start (); } } Class Mythread { int index = 0; Private class Innerthread extends Thread {public synchronized void run () {while (true) { System.out.prin TLN (Thread.CurrentThread (). GetName () + "is running and index is" + index++); }} Public Thread GetThread () { return new Innerthread (); } } In this one, the inner class shares the index variable inside the class common class, and by adding locks to the public class to achieve the purpose of the method synchronization.

(2) Piping

Mainly divided into the following steps:

First, the pipeline flow is established and the input and output objects of the pipeline flow are linked;

Join the pipeline flow to the production object (thread);

The input and output stream is drawn through the pipeline flow, and the flow is manipulated in the thread;

Note: The Read method of the pipeline flow is a blocking method;

public class Communicatewhitpiping {public static void main (string[] args) {/** * Create pipeline output stream */        PipedOutputStream pos = new PipedOutputStream ();        /** * Create pipeline input stream */PipedInputStream pis = new PipedInputStream ();        try {/** * will connect the pipeline input to the output with this process can also be implemented by overloading the constructor */Pos.connect (PIS);        } catch (IOException e) {e.printstacktrace ();        }/** * Create producer Thread */Producer p = new Producer (POS);        /** * Create a consumer thread */Consumer c = new Consumer (PiS);        /** * Start thread */P.start ();    C.start ();    }}/** * Producer thread (associated with a pipeline input) * */class Producer extends Thread {private PipedOutputStream pos;    Public Producer (PipedOutputStream pos) {this.pos = pos;        } public void Run () {int i = 8;        try {pos.write (i); } catch (IOException e) {E.printstacKtrace ();    }}}/** * Consumer thread (associated with a pipeline input) * */class Consumer extends Thread {private PipedInputStream pis;    Public Consumer (PipedInputStream pis) {this.pis = PiS;        } public void Run () {try {System.out.println (Pis.read ());        } catch (IOException e) {e.printstacktrace (); }    }}

(3) by calling the thread's public interface

As shown in the difference between calling the public interface and the actor model, calling the public interface is thread A gets a reference to thread B, and by invoking thread B's method, I want to enter information in B to achieve the purpose of thread messaging, its flaw is also obvious, that is the problem of being stuck in B (the method must be returned in B, The process of a can continue to execute)!

Third, other

As we all know, there are many problems in the multi-threaded system of Java, the memory sharing of communication process often leads to unexpected error, and the synchronization of pipeline flow blocking method and calling interface method is sometimes difficult for concurrent programming, and now the multithreaded framework of Streamline is actor system; in C + + The implementation of the associated package in http://c.chinaitlab.com/example/895427.html C + +, which is the specific description of the actor framework in And in Java, the derived language (in fact, the JVM-based language) Scala also provides the actor mechanism, specifically to carry out the relevant search

Multi-threaded communication methods

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.