sharing global variable?

I have one shared library called data.so.
Also I have two seperate process(A & B) that links to this data.so
In this data.so, I made one global variable “Counter”.

But when processA and processB is linked to data.so it seems to get it’s own local copy of “Counter”. How can I make processA and processB to have identical copy of “Counter” variable in this case? So if ProcessA changes “Counter” processB can also see that change and vise versa.

Thanks

Only way I know is to create share memory.

Maybe thjere is a gcc extention to specify that a variable is “really shared” but I wouldn’t know about that.

I was hoping for a more simple solution, but I guess I could use shared memory.

Thanks

There are many sick & twisted things you could do, but what’s wrong with shared memory?

You can have data.so export a function called “foo *get_shared_foo_var()”, which would return a pointer to an object of type “foo”. Each process simply calls get_shared_foo_var() and they get a pointer to a shared variable of type foo (what could be simpler?).

Better yet, have foo be an opaque type, and have the shared library export a bunch of member access functions that work on foo…

opaque type, miam miam I love those. I’m using opaque type all over the place these days. I write of lot of library stuff, stuf that gets used by co-worker. Using opaque type makes my job so much easier. It prevent people from doing thing I didn’t expected them too. It also force everything to be defined and document. Lovely ;)

Now I’m intrigued, or maybe ignorant for not having written an shared libraries. So what is an “opaque” type?

Basicaly it’s a pointer to an internal structure, but you don’t make it public. Here is a pseudo code example of the user header file would contain:

typedef struct Foo_s *Foo_t;
#define ILLEGAL_FOO NULL
Foo_t OpenFoo(void); // on erreur this will return ILLEGAL_FOO
int CloseFoo( Foo_t );
int DoSomeWork( Foo_t );
int GetResult(Foo_t);

Foo_t is opaque because the user had no idea what it points to, what it contain. As a matter of fact the type of Foo_t is totaly irrelevant, could be an integer for all the user know. The user if force to go through functions to work with the pointer, much like a C++ class would do with it’s private data section. Everything has to go through a nice interface.

In the private header of the library I just include the public header and add this:

#include <foo.h>

struct Foo_s {
char *some_memory;
int some_counter;
int thread_id; // yep the CreateFoo will actually create a thread
};

Oh by the way apparently, user typedef should’t be suffix with _t as its reserved for compiler implementation. But old habit are hard to break.

Mario, ok so if I understand properly, there is no actual type “opaque”, but rather its a technique to hide the internal type?

Yeah I guess you could say that opaque type is just a fancy name for hiding internal stuff. Since C doesn`t have concept of private data I guess “opaque” was chosen to represent the concept ;-)

There is probably lots of way to implement opaqueness ;-)

Good~ perfect.
Thanks for the insight everyone. Can’t wait till I try it.