Board index » cppbuilder » Reading VCL value from a Thread

Reading VCL value from a Thread


2004-10-02 07:49:56 AM
cppbuilder20
I'm having a strange problem.
As i know, there should be no problem READING value of a VCL
component from a thread, but from what i see this is exactly
what happen - I have 2 threads, one of them is calling a
function (without 'Synchronize()' ) that contains the line -
if ( MyListBox )
Number = MyListBox->Items->Count;
else
Number = 0;
(i'm using it in order to know when i get to the maximum number
of lines that a ListBox can contains)
when this line is enable it's cousing the application to frozen,
if i put a breakpoint on this line i can see that on the first
call to the function the value is 0, but when it's getting there
for the second time then instead of showing a value (in the
watch) it it shows me in the watch window: "[Evaluating...]".
If i disable this line then everything runs OK and without
any problem.
What can be the problem here? can't i read VCL value from a
thread?
Thanks,
Ramy
 
 

Re:Reading VCL value from a Thread

"Ramy" < XXXX@XXXXX.COM >wrote in message
Quote
As i know, there should be no problem READING
value of a VCL component from a thread
That is an incorrect assumption to make. The value could be altered while
the thread is still reading it. Always provide adequate synchronizing whe
data is accessible to multiple threads.
Quote
when this line is enable it's cousing the application to frozen,
As well it should. The code is not thread-safe.
Gambit
 

Re:Reading VCL value from a Thread

"Ramy" < XXXX@XXXXX.COM >writes:
Quote
I'm having a strange problem.

As i know, there should be no problem READING value of a VCL
This premise is very, very false. Who told you that?
Quote
component from a thread, but from what i see this is exactly
what happen - I have 2 threads, one of them is calling a
function (without 'Synchronize()' ) that contains the line -

if ( MyListBox )
Number = MyListBox->Items->Count;
else
Number = 0;
when this line is enable it's cousing the application to frozen,
If the object is being updated while you're reading from it, it may be
in an indeterminate state... halfway updated, and totally
inconsistent. Using the values in such an object is disastrous.
--
Chris (TeamB);
 

{smallsort}

Re:Reading VCL value from a Thread

Thanks very mych for the answers! i moved this part to the
'WndProc()' function and now it's running great :-)
Remy, i thought that i can READ value of a VCL component from
a thread after reading some of your messages -
groups.google.com/groups&lr=&ie=UTF-8&selm=3c8e6f6a_1%40dnews
"It's not a matter of which components are used (TFileStream
is not a component, btw). It's a matter of which threads
access the same objects. Any time two or more threads need to
access the same data, whether that be objects or primitives or
whatever, then they need to be synchronized with each other to
ensure that they don't try to access the same memory at the
same time. Reading is not a problem, multiple threads can
read the same memory at the same time safely. It's when
writing to memory is involved that synchronization is needed.
Gambit"
groups.google.com/groups&lr=&ie=UTF-8&selm=3d62fb50%40newsgroups.borland.com
"...Are you using Synchronize() to synch that access? When
accessing data across threads, you must synch it properly to
ensure that the two threads don't try to modify the data at
exactly the same time, otherwise things could blow up very
badly. Remember that multiple threads run side-by-side,
independantly of each other, there's always the potential of
trying to access the same memory at the exact same time.
Simply reading the data is safe, but modifying memory is very
dangerous if it's not protected correctly.
Gambit"
So what did you mean in this messages? maybe i did not
understand.
Thanks,
Ramy
 

Re:Reading VCL value from a Thread

"Ramy" < XXXX@XXXXX.COM >writes:
Quote
Reading is not a problem, multiple threads can read the same memory
at the same time safely. It's when writing to memory is involved
that synchronization is needed.
If ALL threads only read, then the data isn't changing, and so it
shouldn't be a problem. But if there is possibility for any thread to
"do stuff" to the data you're reading, there is a problem.
Admittedly, I'm not familiar enough with the internals of the VCL to
say specifically when it's "ok" to use VCL objects in multiple
threads (nor am I particularly interested) but there are a tiny few
that are safe. Unless you know you're dealing with one of them,
I'd suggest you just play it safe and not push 'til it hurts. A
prudent amount of caution can save you hours in the de{*word*81}. :)
--
Chris (TeamB);
 

Re:Reading VCL value from a Thread

"Ramy" < XXXX@XXXXX.COM >wrote in message
Quote
Remy, i thought that i can READ value of a VCL
component from a thread after reading some of
your messages -
You did not give the messages enough thought. See below.
Quote
Reading is not a problem, multiple threads can read the
same memory at the same time safely. It's when writing
to memory is involved that synchronization is needed.
<snip>
Simply reading the data is safe, but modifying memory is
very dangerous if it's not protected correctly.
You are dealing with VCL properties. The property values can change at any
time. Your thread may only be reading the values, but the main thread can
change the values while your thread is reading them. That is why you must
synch the access.
Gambit
 

Re:Reading VCL value from a Thread

Thanks very much :-)
Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
Quote
"Ramy" < XXXX@XXXXX.COM >writes:

>Reading is not a problem, multiple threads can read the same memory
>at the same time safely. It's when writing to memory is involved
>that synchronization is needed.

If ALL threads only read, then the data isn't changing, and so it
shouldn't be a problem. But if there is possibility for any thread to
"do stuff" to the data you're reading, there is a problem.

Admittedly, I'm not familiar enough with the internals of the VCL to
say specifically when it's "ok" to use VCL objects in multiple
threads (nor am I particularly interested) but there are a tiny few
that are safe. Unless you know you're dealing with one of them,
I'd suggest you just play it safe and not push 'til it hurts. A
prudent amount of caution can save you hours in the de{*word*81}. :)

--
Chris (TeamB);
 

Re:Reading VCL value from a Thread

Now i understand, thanks!
"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Ramy" < XXXX@XXXXX.COM >wrote in message
news:415dfb68$ XXXX@XXXXX.COM ...

>Remy, i thought that i can READ value of a VCL
>component from a thread after reading some of
>your messages -

You did not give the messages enough thought. See below.

>Reading is not a problem, multiple threads can read the
>same memory at the same time safely. It's when writing
>to memory is involved that synchronization is needed.
<snip>
>Simply reading the data is safe, but modifying memory is
>very dangerous if it's not protected correctly.

You are dealing with VCL properties. The property values can change at any
time. Your thread may only be reading the values, but the main thread can
change the values while your thread is reading them. That is why you must
synch the access.


Gambit