Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TStringList and 2 threads

1 view
Skip to first unread message

Denville Longhurst

unread,
Mar 2, 2005, 9:54:36 AM3/2/05
to
Hi

Is it safe for two separate TThreads to access a TStringList (one puts the
string in, the other takes it out)??
There are no VCL calls in either thread, and it seems to work ok.

CPB version 6.

Many thanks,
Denville.


Pete Fraser

unread,
Mar 2, 2005, 10:12:16 AM3/2/05
to
No, I don't think it is without using synchronize().
You may get away with it but it is very dangerous.
For instance, if you are just adding things in the list that
is writing, that *may* be OK as long as the VCL
(TStringList->Items->Add) only updates the count after
adding the string.
However, at some point, the source thread may delete all
the strings and that is where the problems happen
and they will depend on random timings and what the CPU
does with the memory that the StringList has free'd up.
So *don't* do it.

HTH Pete

Vladimir Stefanovic

unread,
Mar 2, 2005, 10:07:47 AM3/2/05
to
You can use 'Critical Sections' to prevent from
concurrent access to the same.

This is the simple code skeleton, which you have
to adjust for your needs.

--- H ---

class TForm1 : public TForm
{

// ...

private: // User declarations
CRITICAL_SECTION MyCriticalSection;

};

class CSLock
{
public:
LPCRITICAL_SECTION fcs;

CSLock( LPCRITICAL_SECTION acs ) : fcs( acs )
{ if ( fcs ) EnterCriticalSection( fcs ); }

~CSLock()
{ if ( fcs ) LeaveCriticalSection( fcs ); }
};

--- CPP ---

__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
InitializeCriticalSection( &MyCriticalSection );
}
__fastcall TForm1::~TForm1()
{
DeleteCriticalSection( &MyCriticalSection );
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
CSLock lock( &MyCriticalSection );
// Here you can do something with string lists
ShowMessage("DoSomethingWithStringList()");
}


--
Best regards,
Vladimir Stefanovic
"Denville Longhurst" <Denv...@metco.co.uk> wrote in message
news:4225...@newsgroups.borland.com...

Denville Longhurst

unread,
Mar 2, 2005, 10:25:21 AM3/2/05
to
Pete,

Thanks for the quick response and the advice.

Sincerely,

Denville.

"Pete Fraser" <pete.no.s...@frasersoft.nospam.net> wrote in message
news:4225d6e8$1...@newsgroups.borland.com...

Denville Longhurst

unread,
Mar 2, 2005, 10:24:27 AM3/2/05
to
Vladimir,

Thank you very much for the quick response and the help.

Sincerely,

Denville.

"Vladimir Stefanovic" <anti...@po.sbb.co.yu> wrote in message

Remy Lebeau (TeamB)

unread,
Mar 2, 2005, 2:14:53 PM3/2/05
to

"Denville Longhurst" <Denv...@metco.co.uk> wrote in message
news:4225...@newsgroups.borland.com...

> Is it safe for two separate TThreads to access a TStringList


> (one puts the string in, the other takes it out)??

If you not synchronizing access to the TStringList, then no, it is not safe.

> There are no VCL calls in either thread, and it seems to work ok.

Always provide adequate synchronizing when sharing memory across threads.


Gambit


Denville Longhurst

unread,
Mar 2, 2005, 6:11:27 PM3/2/05
to
Thanks again.

Sincerely,

Denville.

"Remy Lebeau (TeamB)" <no....@no.spam.com> wrote in message
news:422610a8$1...@newsgroups.borland.com...

0 new messages