Page 1 of 1

can the mainform read a thread's public variable?

PostPosted: May 30th, 2008, 8:09 am
by actalk
Hello!

There are a few thread..at most 10 threads.
They have each own buffer array.
They post their own buffer's current index to mainform
and then mainform access (read) the thread's buffer data.

There is not any Synchronization and not any criticalsection.
is there any problem ? or any suggestion?
Thanks in advance.

code like this as summary:

type
TplThread = Class(TThread)
..
..public
....Buffer:array[0..100, 0..BufSize] of char;
..

on thread execute
..ReadFile(Pipe1, Buffer[currentBufferIndex, 0], BufSize, BytesRead, nil);
..
..postmessage(threadid, currentBufferIndex) to main form
..

on MainForm
..memo.Lines.Add(thread1.Buffer[currentindex]);
or
..write thread1.Buffer[currentindex] to a logfile...

-0-

PostPosted: May 30th, 2008, 11:23 am
by Kambiz
The code has synchronization problem. You must access the global thread variables within a critical section.

PostPosted: May 31st, 2008, 4:50 pm
by actalk
form1= TForm
..public
....cs: TCriticalSection;
.
constructor form1.Create
..cs := TCriticalSection.Create;
.
.
proc form1.startThread
..cretaeThread(cs)
..
Threads will use form's public variable cs (TCriticalSection) to lock code parts...
is there any problem?

PostPosted: June 1st, 2008, 8:37 am
by Kambiz
It's correct. The CS veriable should be shared with the form and the threads.