Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

A mutex with shared variables

Posted on 2000-04-08
11
Medium Priority
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

636 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