C Linux: Global variable located in shared library as singleton

后端 未结 3 1878
醉梦人生
醉梦人生 2021-02-15 16:43

Is it possible to use global variable located in a shared library (.so) as a singleton if the library is used by more than one process?

As example the initial value is 0

3条回答
  •  渐次进展
    2021-02-15 17:05

    If a shared library (or a Windows DLL) is used by more than one process, any modifyable data is still private to the process. There are mechanisms like Copy on Write where the same data is shared as long as it is only read, but copied as soon as it is written by either process. So, for each process the data effectively is still separate. See also shared library address space.

    If you want to share data between processes, you need to use Shared Memory and make sure that access to the shared memory is synchronized between the processes.

提交回复
热议问题