2

Suppose I have a library with a global variable which is accessed for both Read and Write operations.

I am assuming the following:

  • A statically-linked library will not be safe-to-use concurrently on different threads.
  • A statically-linked library will be safe-to-use concurrently on different processes.
  • A dynamically-linked library will not be safe-to-use concurrently on different threads.
  • A dynamically-linked library will not be safe-to-use concurrently on different processes.

Are the assumptions above correct?

If it matters anything (although I suppose it doesn't), then I am coding in C++ and running over Windows.

Thanks

barak manos
  • 29,648
  • 10
  • 62
  • 114

2 Answers2

2

Your last assumption is wrong, you cannot accidentally share data between libraries.

How this is achieved is specific to each library format and operating system, but the main idea is simple:

  • code is read-only: it can be shared safely (think int rand() { return 4; })
  • constants are read-only: they can be shared safely (think "Hello, World!")
  • variables are not read-only: they are not shared (an immutable "template" is shared, and used to initialize the process own copy which is private)

Even when using fork on Linux, the newly created process will not share the variables from its parent process; it will share their initial value in a copy, but both will then evolve differently.

That being said, just avoid global variables; and if possible also avoid thread-local variables.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • So what you're basically saying is that the code-section is shared, but the two other sections (data-section and stack) are not shared? So a part of the DLL image is in fact "replicated" and linked statically to the executable? – barak manos Mar 08 '14 at 14:24
  • DLL... are we speaking of Windows? I don't expect much difference, but I know it less... on Linux the shared library is loaded once and subsequent processes maps its space to their own. So it's shared. Only when a process writes (where permitted - so data segments only) to the shared space, that memory page gets duplicated for its "personal" use. It works at a lower level. See this [SO question/answers](http://stackoverflow.com/questions/4022127) – Sigi Mar 08 '14 at 14:33
  • @barakmanos: yes, that is what I am saying. Although, to be precise, an immutable version of the data section is shared, and used as a seed to initialize the data section of each process. – Matthieu M. Mar 08 '14 at 14:37
  • @Sigismondo: As mentioned in my answer, I am speaking neither about a particular format nor about a particular OS; just outlining the basic idea behind shared libraries. How the actual sharing is achieved can get very intricate and I have little to no interest to get down to such level. – Matthieu M. Mar 08 '14 at 14:39
  • I meant to answer to *part of the DLL image is in fact "replicated" and linked statically to the executable?* **No**: it's not statically linked, it's not included at compile time. It's in the shared library but... and so on - so he was asking on the mechanism. – Sigi Mar 08 '14 at 15:02
  • @Sigismondo: thanks. After posting the question, I did realize that I should mention "Windows" next to that "C++" line at the end of the post. Will add it, though (as that line states), I don't think it's supposed to make a difference. – barak manos Mar 08 '14 at 15:49
  • @barakmanos: the principle does not vary, but the particulars (implementation) will. – Matthieu M. Mar 08 '14 at 15:52
  • Thanks. Your first comment should probably be added to your answer, for other people to benefit from (though I will accept it in either case). Thanks for the clarifications :) – barak manos Mar 08 '14 at 15:56
1

A dynamically-linked library will not be safe-to-use concurrently on different processes.

This is false.

Even if the library is shared among different processes, this regards the code. But each process map data to its memory space. So each process have independent global variables, even if they come from a shared library.

To be more precise, here it's been explained with good detail.

To let different processes share some memory you need to use specific libraries, as shmget() or shm_open()

Community
  • 1
  • 1
Sigi
  • 4,826
  • 1
  • 19
  • 23