Link to home
Start Free TrialLog in
Avatar of myson
myson

asked on

A mutex with shared variables

Hello!
I have a DLL library with shared variables (2 structures and some ints). Must I use mutex object to ensure that when I am writing a shared structure, other instance of my DLL is not reading it (like multithread apps)?

Thanks.
Avatar of ufolk123
ufolk123

Not require for a ordinary global/static variable in a DLL.As the data section of a DLL is different for each processes linked to DLL.So if you have a global variable var1 in a DLL then tweo programs A & B using that DLL will have different storage for var1.
So if your taget apps using DLL are single threaded there is nothing to worry about.If your app is multithreaded then DLL global variable is as unsafe as a process global variable.
Tell me if you have any more doubts.

Avatar of myson

ASKER

Sorry, but you are not following my way. I am not talking about global variables, but about shared variables:

#pragma comment (linker, "/SECTION:.shared,RWS")
#pragma data_seg(".shared")
//shared variables
#pragma data_seg()

I'm using a global hook, and in a DLL I have a shared section with structures and variables like the hook handle, ...
If I write or read to/from these variables, have I to use a mutex?
Not necessary until only one thread is accessing those variables  at a time.

Mutex and Semaphores are usually synchronization objects, u use them only when u want to synchronize multiple threads...via a shared memroy

Since ur accessing it in only one thread...u need not use mutex.....but in case if plan to use in more than one thread in the near future ....then u should go in for either a Mutex or Semaphore...

Hope this info proves useful to u.
Regards,
Vinod
Another thing i forgot to ask u a thing.

why use Shared varaibles.....when ur not accessing them in other threads...u can just make them global.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
nietod,
i know that we use a mutex for syncronizing processes thats why i said he wont need it.......

Did u read myson's comment:
"other instance of my DLL is not reading it (like multithread apps)? "

and as far as hook procedure is concerned myson said that he/she has declared it as a global and not shared......so no point of it being acessed in other threads/processes

a seperate copy of global hook will be created for every new process/thread...

Anyways thanx for ur advice....

regards,
Vinod


>> Did u read myson's comment:
>> "other instance of my DLL is not reading it
>> (like multithread apps)? " 
except that isn't true and myson actualy seems to know that.  Other instances can as it is used in a hook.  It will be loaded into every process in the system and all the processses will have access to that single copy of the shared memory.  Efforts must be made to insure that simultaneous access does not occur.  That may actually result from the hook design itself, but I don't know for sure, so in doubt I would use a mutex to be save.

>> a seperate copy of global hook will be
>> created for every new process/thread...
I'm not sure what you mean by that, but I think it probably isn't right.  The DLL will be mapped into every processs.  There wil be 1 copy of the code mapped into each process (not that that matters really).  There will be (effectively) sepperate copies of the DLL data mapped into each process--EXCEPT for the shared data.  That will have only one copy.  That is where the danger is.

nietod ,
sorry there was a bit mis-understanding by me, i had read myson's comment incompletely...

he said,
"I'm using a global hook, and in a DLL I have a shared section with structures and variables like the hook handle, ...
"

i just read the first part
"I'm using a global hook"

ya i know that the same copy of variables are used in case of shared variables.

I really want to apologize for that vague comment made by me.....

regards,
Vinod.
Avatar of myson

ASKER

Ye, thanks for the answer(s). Because I am using a GetMsg hook, there is a little probability, that one instance of DLL is paused in the middle of writing to a structure and the other instance is now reading the same structure.
Thanks.
vinodkarande,
No problem.  Half of being an expert is figuring out what a questions mens, i,e, figuring out what is important, what isn't, and what is a mistake.
nietod,
thans for ur sugesstion. I will always keep ur words in my mind.....

regards,
vinod