Solved

C++ Builder Multi-threading

Posted on 1999-01-15
8
1,006 Views
Last Modified: 2011-09-20
__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.  
0
Comment
Question by:schmim04
  • 4
  • 2
  • 2
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1182304
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
 
LVL 22

Expert Comment

by:nietod
ID: 1182305
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
 
LVL 1

Author Comment

by:schmim04
ID: 1182306
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:nietod
ID: 1182307
>> 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
 
LVL 10

Expert Comment

by:viktornet
ID: 1182308
In OnCreate() it would cause problems because the TEdit boxes still have not been created...

-Viktor
--Ivanov
0
 
LVL 22

Expert Comment

by:nietod
ID: 1182309
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1182310
Yeah, OnShow() is okay, but OnCreate() would generate a GPF...

-Viktor
--Ivanov
0
 
LVL 1

Author Comment

by:schmim04
ID: 1182311
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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question