Lib1[dll] { class A { static int i=0; } } Program1[exe] have reference to Lib1 { Class B { main() { A.i = 5; } } } Program2 [exe] have reference to Lib1 { Class C { main() { A.i = 5; } } }
If Program1 and Program2 are executing simultaneously, is it possible that they reference 开发者_如何学Goto a single instance of Lib1 and change made to the static variable of A.i in Program1 are available to Program2 and viceversa,
In general, no, what you are asking for is not possible or recommended. In most operating systems (Windows, Linux, etc), each program instance runs in a separate process address space which is isolated from all other processes. In some cases, the read-only executable code of shared DLLs may be shared between processes to reduce overall memory consumption, but the writable data is local to each process.
You can achieve what you're asking for by making use of OS services to explicitly set up a shared memory area that can be accessed by multiple processes. In Windows, this can be done by creating named shared memory objects, using a name that is known in advance by all participants. You can then typecast that memory block to a structure type and read and write fields in that memory area, and all processes that have a view onto that shared memory will see the same data.
Since multiple processes are running concurrently, you will also need to think about how the data in the shared memory area is updated. If multiple processes need to update a counter field or whatnot in the shared memory area then you need to implement thread safe practices around read and write of that data, such as interlocked increment or using a named mutex object as an exclusive access lock.
精彩评论