Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ Builder Multi-threading

Posted on 1999-01-15
8
Medium Priority
?
1,020 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 600 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

719 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