Solved

C++ Builder Multi-threading

Posted on 1999-01-15
8
1,009 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 92
How to print into std::string like sprintf using C++11? 12 191
Issues with C++ Class 19 121
DCT of  2D array using fftw in c++ 9 134
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

759 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