Click to See Complete Forum and Search --> : Compare Critical Section & Semaphore


stans
December 30th, 2004, 11:19 PM
I am creating a mulithreaded program, I have the thread class. from the main thread i need to initialize 2 threads and in the constructor i need to pass in the thing for synchronization.
Now i have critical section and semaphore can choose. Semaphore is ok by passing the HANDLE to the thread class and it works quite ok.
But When i pass the &Citical_Section to the 2 Threads, it doesn't work well as the 2 threads can access the critical section at the same time....
I know critical section doesn't support the different processes. but i think this is one program, should be in the same process bah..
Is there anyone know can i use the critical section in this way? or should i pass anything else not the critical section address??

mrdave2
December 30th, 2004, 11:39 PM
Well, to use a critical section between threads, its easy. Declare the critical section object as a global variable, then in each thread before you try to access whatever shared resource ( ? ), do:

EnterCriticalSection( &sectobj );
// ... access stuff here
LeaveCriticalSection( &sectobj );

BTW: You first have to initialize the critical section object. You only do this once, and can do it anywhere - Just do it before you start using it.

InitializeCriticalSection( &sectobj );

Then, when your done, free the resources it takes up and call:

DeleteCriticalSection( &sectobj );

Never had a problem with it once I got it down. Infact, I think Critical Sections are the easiest way to synchronize threads.

stans
December 31st, 2004, 01:27 AM
Now the code is like this:
In Main{
create Critical_Section cs;
InitializeCriticalSection( &cs);
new Thread one (passin &cs);
new Thread two (passin &cs);
}

in Thread Class{
constructor(get &cs)
EnterCriticalSection( &cs);
// ... access stuff here
LeaveCriticalSection( &cs);
}

seems the 2 threads still can access the critical section at the same time.....

is this the way to do the critical section??

Thanks a lot for your help

cody123456
December 31st, 2004, 03:49 AM
Hi,
yep, you can do like this. Remember to add also deinitialization of critical section. But usually you will use CriticalSection in class thats instance can be accessed from both threads and not give it directly to threads.

But be carefull to use locks only where you need it (accessing shared data),
otherwise you can have deadlock situations. And now is time also to
start generating debug traces WITH timestamps, these really help to analyze
multithreaded apps and minimize all interaction between threads.

Multithreadin is perfect way to produce apps that work 99.9% of time, so
you wont notice that you have problem untill it is too late. So be paranoid!

class ThreadSafe
{
ThreadSafe(){
//init cs
}
~ThreadSafe(){
//deinit cs
}

SetData(int iData){
cs.Lock()
m_iData = iData;
cs.Unlock()
}

int GetData(){
int iTmp;
cs.Lock()
iTmp = m_iData; //Get local copy of data
cs.Unlock()
return iTmp; //return local copy
}

CRITICAL_SECTION cs

//You can use arbitary datastruct here
int m_iData;
}

myron
October 17th, 2005, 10:45 PM
Now the code is like this:
In Main{
create Critical_Section cs;
InitializeCriticalSection( &cs);
new Thread one (passin &cs);
new Thread two (passin &cs);
}

in Thread Class{
constructor(get &cs)
EnterCriticalSection( &cs);
// ... access stuff here
LeaveCriticalSection( &cs);
}

seems the 2 threads still can access the critical section at the same time.....

is this the way to do the critical section??

Thanks a lot for your help

M... I"m curious about this code. It seems it's ok. Anyone knows why CS doesn't work here?

Marc G
October 18th, 2005, 01:55 AM
[ moved thread ]

Marc G
October 18th, 2005, 01:58 AM
M... I"m curious about this code. It seems it's ok. Anyone knows why CS doesn't work here?
What is wrong with it?