Solved

A mutex with shared variables

Posted on 2000-04-08
11
274 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now