Board index » cppbuilder » Do i need Synchronize or TCriticalSection

Do i need Synchronize or TCriticalSection


2004-10-31 01:23:30 AM
cppbuilder60
Hi,,
in my application i have many threads which does calculations. now i like to
get an average time of all calculations.
my idea is to create a double variable in my main form. and earch thread
should add some value to this variable.
the problem is, that sime threads may do this at the same time and there
would be some problems. now i read about Synchronize and TCritialSection.
But i found no differenze und do not know would be good for me.
so, will i get problems, when each thread use this code?
void __fastcall TMyThread::AddValue()
{
Form1->Value += value;
}
void __fastcall TMyThread::Execute()
{
...
Synchronize(AddValue);
...
}
Or do I need TCriticalSection. How should i implement it, when i need?
Tommy
 
 

Re:Do i need Synchronize or TCriticalSection

If you decide to use CriticalSections, maybe this
way:
--- global ---
CRITICAL_SECTION MyCriticalSection;
--- constructor ---
InitializeCriticalSection( &MyCriticalSection );
--- destructor ---
DeleteCriticalSection( &MyCriticalSection );
--- code that adds value ---
void __fastcall TMyThread::AddValue()
{
try
{
EnterCriticalSection( &MyCriticalSection );
Form1->Value += value;
}
__finally
{
LeaveCriticalSection( &MyCriticalSection );
}
}
Also there is some other (more elegant and secure) way
Remy has once supply for me (this is it without any changes)
--- *.h ---
CRITICAL_SECTION MyCriticalSection;
--- *.cpp ---
class CSLock
{
public:
LPCRITICAL_SECTION fcs;
CSLock(LPCRITICAL_SECTION acs) : fcs(acs)
{ if( fcs ) EnterCriticalSection(fcs); }
~CSLock()
{ if( fcs ) LeaveCriticalSection(fcs); }
};
//...
void __fastcall TserverForm::ServerSocketClientRead(TObject
*Sender,
TCustomWinSocket *Socket)
{
// ...
CSLock( &MyCriticalSection );
MyStringList->Add ( "Is this now secured adding?" );
// ...
}
Best regards,
Vladimir Stefanovic
 

Re:Do i need Synchronize or TCriticalSection

"Vladimir Stefanovic" < XXXX@XXXXX.COM >wrote in message
Quote
If you decide to use CriticalSections, maybe this way:
To expand on that, since all of the threads are going to a single value, a
singleton class may be more useful here:
class CSLock
{
private:
LPCRITICAL_SECTION FCS;
public:
CSLock(LPCRITICAL_SECTION ACS) : FCS(ACS)
{ if( FCS ) EnterCriticalSection(FCS); }
~CSLock()
{ if( FCS ) LeaveCriticalSection(FCS); }
};
class CalcAverage
{
private:
CRITICAL_SECTION FCS;
double FValue;
CalcAverage();
public:
~CalcAverage();
static CalcAverage* Instance();
void IncValue(double Val);
double GetValue();
// whatever else you need...
};
CalcAverage::CalcAverage()
{
InitializeCriticalSection(&FCS);
}
CalcAverage::~CalcAverage()
{
DeleteCriticalSection(&FCS);
}
CalcAverage* CalcAverage:Instance()
{
static CalcAverage inst;
return &inst;
}
void CalcAverage::IncValue(double Val)
{
CSLock lock(&FCS);
FValue += Val;
}
double CalcAverage::GetValue()
{
CSLock lock(&FCS);
return FValue;
}
void __fastcall TMyThread::AddValue()
{
CalcAverage::Instance()->IncValue(value);
}
Gambit
 

{smallsort}

Re:Do i need Synchronize or TCriticalSection

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
CalcAverage::CalcAverage()
{
InitializeCriticalSection(&FCS);
}
Minor addition:
CalcAverage::CalcAverage()
: FValue(0.0) // <-- here
{
InitializeCriticalSection(&FCS);
}
Gambit
 

Re:Do i need Synchronize or TCriticalSection

Hi,
this looks great, could you give me please also an exmaple, how i can use
this class from my threads (only add the value) and how i can use the class
from my main form (only read).
i have no idea for that.
tommy
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
Quote

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
news:4183ddf6$ XXXX@XXXXX.COM ...

>CalcAverage::CalcAverage()
>{
>InitializeCriticalSection(&FCS);
>}

Minor addition:

CalcAverage::CalcAverage()
: FValue(0.0) // <-- here
{
InitializeCriticalSection(&FCS);
}


Gambit


 

Re:Do i need Synchronize or TCriticalSection

"Tommy" < XXXX@XXXXX.COM >wrote in message
Quote
this looks great, could you give me please also an exmaple, how
i can use this class from my threads (only add the value)
I already gave you such an example.
Quote
and how i can use the class from my main form (only read).
Simply call GetValue() the same way that IncValue() is being called:
void __fastcall TMainForm::DoSomething()
{
double d = CalcAverage::Instance()->GetValue();
// use d as needed...
}
Gambit