8

I am sharing some data across multiple processes by using shared memory; I use inter processes mutexes to achieve synchronization.

My question is the following: is it possible to use lock-free data structures AND/OR atomic operations to achieve faster synchronization without using mutexes between 2 processes?

If not do you know what is the main reason for this?

They are used only to synchronize threads of the same process. Are these concepts portable to processes as well? If they aren't do you know any faster method to share/synchronize data across processes?

1
  • You will probably find this just makes performance worse. Though don't use inter-process synchronization primitives for threads in the same process. The synchronization primitives are pretty close to the least you can possibly have on x86 and get any synchronization at all. Nov 17, 2011 at 0:04

2 Answers 2

10

Are these concepts portable to processes as well?

Yes, atomic operations are universal both for threads and processes, IIF the memory atomically used is shared.

Atomic operation is specific instruction of processor itself and in knows nothing about threads or processes, it is just All-or-nothing (indivisible) complex of actions (read; compare; store) with low-level hardware implementation.

So, you can setup shared memory between processes and put an atomic_t into it.

lock-free

Yes, if lock-free is implemented only with atomic. (It should)

data structures

You should check, that shared memory is mapped to the same address in both processes when it is used to store pointers (in data structures).

If the memory will be mapped to different address, pointers will be broken in another process. In this case you need to use relative addresses, and do simple memory translation.

inter processes mutexes

And I should say that glibc>2.4 (NPTL) uses futex combined with atomic operations for non-contended lock (for Process shared mutexes = inter process mutexes). So, you already use atomic operations in shared memory.

3
  • Thanks osgx. Do you think that it might work if in shared memory I store simple integers to be read/written via __gcc_sync_fetch_and_xxx operations? If I am not wrong atomic_t is not available anymore like it was in the past in atomic.h. Nov 17, 2011 at 0:09
  • Yes, __sync are atomic builtins. Don't know about atomic_t availability, but you should know, which sizes of data are allowed to be used in atomic operation and what is alignment for it (this varies from one cpu to another, even in Intel Pentium3-4 vs Core2).
    – osgx
    Nov 17, 2011 at 0:20
  • 2
    Hmm, __sync are legacy and __atomic should be used
    – osgx
    Nov 17, 2011 at 0:23
2

On x86 with NPTL, most of the synchronization primitives have as their fast path just a single interlocked operation with a full memory barrier. Since x86 platforms don't really have anything lighter than that, they are already about the best you can do. Unless the existing atomic operations do exactly what you need to do, there will be no performance boost to pay back the costs of using the semantically lighter primitive.

10
  • If he uses mutex to protect some shared data, there will be lock and unlock of mutex for each access. And if data is short (this is not true for all types of usage, but is for some), it can be changed with single atomic operation, which is twice lighter than fastpath (not counting default mutex logic: switch between types of mutex; jumps from selecting fast/slow path).
    – osgx
    Nov 17, 2011 at 0:17
  • Hi David. Let me check If I understood correctly: you are saying that if I have to strictly "LOCK"/"syncronize" an area of code, linux posix locking primitives are already the fastest/best solutions available.. BUT if I want to strictly do an add/xor/and etc.. the atomic operations __sync_fetch_and_xxx are faster. Am I correct? Nov 17, 2011 at 0:20
  • @osgx It's the same. He'd have one interlocked operation for the lock and none for the unlock. The rest of the code optimizes to nearly nothing. Nov 17, 2011 at 0:33
  • @AbruzzoForteeGentile If the atomic operations do exactly what you need, they will be faster. But if you have to fake the fact that they don't, locks will usually be faster. Nov 17, 2011 at 0:34
  • 1
    "The rest of the code optimizes to nearly nothing"... Optimized by compiler? CPU? How PLT is Optimized out? How jmps are optimized out? How (atomic instruction + 2 branch + PLT call) = mutex can be faster than (atomic instruction) ? It can be same order of speed, but it is bit slower.
    – osgx
    Nov 17, 2011 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.