C++ Builder Multi-threading

__fastcall TSomeThread::TSomeThread(bool CreateSuspended): TThread(CreateSuspended)
 {
  Priority = tpNormal;
 }
void _fastcall TSomeThread::Execute()
 {
 }

Here is the basic code for my thread object TSomeThread.  Let's say I have a TForm with a bunch of TEdit boxes on it and I want a thread that does some calculations and continuously writes the data to these TEdit boxes while the TForm is open.  In other words, the display is "live".  Do I need to do a Synchronize for everyone of my TEdit boxes since they're all objects?  If not, what do I do and where would I call my Execute() function so it starts as soon as this TForm is openned with a TButton push that calls TForm->Show()?

My question is a bit vague, but I need as much info on multi-threading as possible.  
LVL 1
schmim04Asked:
Who is Participating?
 
nietodCommented:
Yes, you will need to synchronize access to the TEdit boxes.  One possible problem (without snchronization) would be that as one thread attempts to display the data, another thread may attempt to change it.  At the very least, this could result in the data being displayed incorrectly.  However it could result in a crash.

0
 
nietodCommented:
You may not have to synchronize each edit box seperately.  (If it would be to maintain all those mutexes (or whatevery your are using))  You could use a single synchronization object to synch access to all the boxes.  That is, the thread that "owns" the object can access ANY of the edit boxes and the other thread cannot access any. (This is a little less efficient, but it may be good enough and is easier to manage.)
0
 
schmim04Author Commented:
Ok, so I have to synchronize the TEdit boxes.  There are 40 of them on the TForm.  Will this cause problems since there are so many?  Maybe I could just synchronize one at a time like you say, and just index through all 40 one at a time, etc?  If I do this, how long will the delay be between TEdit box updates?
My other initial question was, where do I call the Execute() method of the thread object (the one that does calculations, then writes the data to the 40 TEdit boxes), so that the thread starts as soon as the TForm is openned?  Maybe I could start the thread when the TButton that opens the TForm (TForm->Show()) is clicked?  How would I do that?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
nietodCommented:
>> There are 40 of them on the TForm.  Will this cause problems since there are so many
In terms of OS resources, you should have no problem having 40 synch objects (critical sections would be best, mutexes are slower). The only issue is whethor not you want to go to the trouble of managing them.  Your code would have to allocate them and destroy them, and when you need to lock one, you need find the right one to lock, probably from an array.  It is more work, so it depends on whethor or not it is worth it to you.  It will potentially be faster if you have 40 seperate synch objects.

>> If I do this, how long will the delay be between TEdit box updates?
I would have no idea.  That depends on what you are doing.

>>My other initial question was, where do I call the Execute() method
I hadn't seen that.  (And by the way, it is a bad idea to ask two questions in one EE question, (in fact it is against the rules, because it can cause confussion when an twi experts answer the deperate parts)  I'm not too familiar with VCL, but I would say you should do it in either the OnShow() or OnCreate() member functions.
0
 
viktornetCommented:
In OnCreate() it would cause problems because the TEdit boxes still have not been created...

-Viktor
--Ivanov
0
 
nietodCommented:
Are you sure of that?  (I have no idea as I don't use builder, but in my class library the child window objects are accessible in the OnCreate procedure.  That is because the child window objects exist even-though the child windows don't).  If not I assume OnShow() is still okay?
0
 
viktornetCommented:
Yeah, OnShow() is okay, but OnCreate() would generate a GPF...

-Viktor
--Ivanov
0
 
schmim04Author Commented:
Is there a C++ Builder book for people who are more advanced programmers?  I program in Builder all the time, but whenever I try something new (threads) I always seem to have minor problems that can be solved in 2 minutes if I had the right resources.  Every book I've ever seen spends 33% of its time on C/C++ reveiw, 33% on suff most average users already know, and the last 33% on stuff that is of little use.  Thanx for the help.  If you think of more, please comment.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.