Solved

A mutex with shared variables

Posted on 2000-04-08
11
278 Views
Last Modified: 2010-04-10
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
Comment
Question by:myson
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 3

Expert Comment

by:ufolk123
ID: 2695778
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
 

Author Comment

by:myson
ID: 2695793
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
 

Expert Comment

by:vinodkarande
ID: 2695882
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Expert Comment

by:vinodkarande
ID: 2695887
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
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2695898
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
 

Expert Comment

by:vinodkarande
ID: 2695929
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
 
LVL 22

Expert Comment

by:nietod
ID: 2695972
>> 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
 

Expert Comment

by:vinodkarande
ID: 2695985
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
 

Author Comment

by:myson
ID: 2696038
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
 
LVL 22

Expert Comment

by:nietod
ID: 2696078
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
 

Expert Comment

by:vinodkarande
ID: 2699580
nietod,
thans for ur sugesstion. I will always keep ur words in my mind.....

regards,
vinod
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question