Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Critical Sections....

Status
Not open for further replies.

MarkLaz

Technical User
May 20, 2005
12
GB
Good Morning (afternoon/evening) All,

I am writing some code that explicitly accesses a piece of hardware on the PCI slot. This hardware can only be written to/ read from by one function/process/thread at a time.

Due to the complexity of the program, I can't really put all the read and writes in one function, with an EnterCriticalSection() at the beginning to only allow one calling thread access at a time.

So my question:

Can I use EnterCriticalSection() with the same Critical section object in more than one place? ie:

void SomeFunction()
{
EnterCriticalSection(CritSec);
...do stuff here
LeaveCriticalSection(CritSec);
}

void AnotherFunction()
{
EnterCriticalSection(CritSec);
...do other stuff here
LeaveCriticalSection(CritSec);
}

Will this provide the behaviour I'm after, or am I barking up the wrong tree?

Cheers...

Mark
 
What I do is to construct the critical section in a constructor and destroy it in a destructor then use a local object as a "lock". The critical section is entered on leaving the locks constructor and is automatically destroyed at the end of the block. This is a form of the technique called RAII (resource acquisition is initialisation).
Something like (in pseudocode)
class Lock
{
private:
CRITICAL_SECTION lock;
public:
Lock() { // enter crit sec.}
~Lock() { // leave crit sec}
};

void threadsafe func()
{
Lock thelock; // enters crit sec
// atomic actions
} // crit sec left here.
 
1. Yes, you can (in a single process, i.e. in your application only, because of a critical section is not a global object).
2. It seems CodingNovice's solution is useless because of this critical section exists only in a function call context and does not protect other threads (with its own exemplares of Lock objects)...
 
Thanks both for your input. ArkM I think has provided what I need, but both opinions are appreciated!

Cheers...
Mark
 
have you considered using events instead?
i think what you have should do the trick, but somehow (i don't really have a good reason!) i don't like critical sections. i realize they are better sometimes.. but i just dno't like them :p i prefer using events...
 
Hi drewdaman,

Yes, I considereed using events. The problem is that I'm altering code that was originally used to send this information via ethernet. As such I have to stick to the original architecture, which uses critical sections (without duplicating them as I questioned here). The code is in a fair old mess,so keeping track of events would end up a nightmare in it self, as so many functions would be generating them. I'm not hugely comfortable with critical sections myself, but it looks like this is the way it has to be done!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top