Anti-Block Messaging

What is the difference between concurrency message passing schemes and lock-based concurrency schemes in terms of performance? A thread that is waiting for locks, so other threads can execute. As a result, I don’t see how messaging can be faster than concurrency locking.

Edit: In particular, I am discussing a messaging approach, as in Erlang, compared to a shared data approach using locks (or atomic operations).

+6
source share
5 answers

As some others suggested ("apples and oranges"), I view these two methods as orthogonal. The assumption assumed here is that one chooses one or the other: we will use locks and shared resources, or we will use messaging, which makes the other unnecessary, or perhaps the other is not even available.

Like, say, a metacricular evaluator, it is unclear what the real primitives are here. For example, to implement messaging, you probably need atomic CAS and some semantics of memory visibility, or perhaps some lock and general state. You can implement atomic operations in terms of locks or implement locks in terms of atomic operations (as Java does in java.util.concurrent.locks ).

Similarly, although, admittedly stretched, it is possible to implement blocking with the transmission of messages. To ask which one works best does not make much sense in general, because it really is more a question of which ones are built in terms. Most likely, one that is at a lower level may be better managed by a programmer than one that is built at the top, as was the case with manual transmissions until recently (quite a bit of debate there too).

Typically, the messaging approach is not welcomed for better performance, but rather for security and convenience, and it usually sells, depriving the programmer of control over locking and sharing. As a result, he stakes against the programmer's capabilities; if the programmer cannot get the lock, he cannot do it badly and slow down the program. Many, like discussions about manual memory management and garbage collection, some argue that they are "good drivers", making the most of manual control; others, especially those that implement and promote the use of the garbage collector, argue that collectively, the collector can do a better job than the "not-so-good drivers" with manual control.

There is no absolute answer. The difference here will be related to the level of skills of programmers, and not with the tools that they may possess.

+8
source

IMHO, messaging is probably not exactly a concurrency scheme. This is basically a form of Inter-Communication (IPC), an alternative to shared objects. Erlang simply approves the passing of messages to shared objects.

Disadvantages of common objects (passing message passing):

  • The state of Mutable / Shared objects is more difficult to argue in context when multiple threads are running at the same time.
  • Synchronization on shared objects will result in algorithms that by their nature are not wait free or not lock free .
  • In a multiprocessor system, a shared object can be duplicated in processor caches. Even using Compare and swap algorithms that do not require synchronization, it is quite possible that many processor cycles will be spent sending messages about cache coherence to each processor.
  • A system built from messaging semantics is inherently more scalable. Since messaging implies that messages are sent asynchronously, the sender does not need to block until the receiver acts on the message.

Pros of common objects (cons of message passing):

  • Some algorithms are usually much simpler.
  • A messaging system that requires blocking resources will ultimately go into common object systems. This sometimes appears in Erlang when programmers start using ets tables, etc. To store the general condition.
  • If the algorithms are not available, you will see improved performance and reduced memory, since there are much less placement of objects in the form of new messages.
+4
source

Using messaging when all you want to do is lock is incorrect. In these cases, use a lock. However, messaging gives you much more than just locking - as the name suggests, it allows you to send messages, i.e. Data between threads or processes.

+1
source

Messaging (with immutable messages) is simplified. With locking and general mutable state, it is very difficult to avoid concurrency errors.

As for performance, it's best to measure it yourself. Each system is different: what are the characteristics of the workload, whether the operations depend on the results of other operations, or are completely or partially independent (which allows the use of massive parallelism), is later or bandwidth, how many machines are there, etc. Locking can be more fast, or again a message or something completely different can be transmitted. If the same approach as in LMAX approaches this problem, perhaps it could be. (I would classify the LMAX architecture as messaging, although it was very different from actor-based messaging.)

+1
source

Messaging does not use shared memory, which means that it does not need locks, so each thread (process) can load or store its own memory, since they exchange with each other - it is to send and receive messages.

0
source

Source: https://habr.com/ru/post/895543/


All Articles