Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

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.
0
myson
Asked:
myson
  • 5
  • 3
  • 2
  • +1
1 Solution
 
ufolk123Commented:
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.

0
 
mysonAuthor Commented:
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?
0
 
vinodkarandeCommented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vinodkarandeCommented:
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.
0
 
nietodCommented:
Myson,

   Yes, In general you must take steps to insure that shared variables in a DLL will not be accessed simultaneously by seperte processes!!!  

   It is possible that it is not necessary to do this for a hook procedure, because it is possible that the hook callback will never be called simultanesously for more than one processed.  But I don't know if that is true or not, so I would not take any chances.

vinodkarande,
    In Widnows you woudl use a critical section to snchronize access to shared data from multiple threads.  You use a mutex for syncronizing processes.  This is a case where he must schncronize processes, so he must use a mutex.  
0
 
vinodkarandeCommented:
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


0
 
nietodCommented:
>> 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.

0
 
vinodkarandeCommented:
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.
0
 
mysonAuthor Commented:
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.
0
 
nietodCommented:
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.
0
 
vinodkarandeCommented:
nietod,
thans for ur sugesstion. I will always keep ur words in my mind.....

regards,
vinod
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now