Solved

C++ Builder Multi-threading

Posted on 1999-01-15
8
1,003 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now