U
U
Ukrainskiy2021-11-16 15:06:08
C++ / C#
Ukrainskiy, 2021-11-16 15:06:08

How to make a restriction on the parallel execution of two methods from different processes?

There are 2 class methods foo and bar, many processes are running at the same time. The foo method can be executed in parallel by multiple processes, the bar method must only be executed by one process at a time if no process is executing foo or bar, and block calling foo until bar is finished. What low-level means can set this behavior? The project uses shared memory.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Dubrovin, 2021-11-16
@Ukrainskiy

You can use:
1. Named semaphores - "out of the box" designed for your task
2. Unnamed semaphores + shared memory (for 1 and 2 there are examples in https://stackoverflow.com/questions/8359322/how-to... )
3. Atomic variables in shared memory (example in https://stackoverflow.com/questions/48614784/alloc... ) + futex (futexes are in Linux, but they can only be used through a system call if something has not changed ) is the most efficient way, because the system call twitches only if a process lock is required, but it is poorly documented, if you are interested, look for implementations of the critical section through the futex, you need about the same
4. mutexes in shared memory with PTHREAD_PROCESS_SHARED

V
Vasily Bannikov, 2021-11-16
@vabka

Mutex

U
Ukrainskiy, 2021-11-16
@Ukrainskiy

https://ru.wikipedia.org/wiki/%D0%91%D0%BB%D0%BE%D...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question