|
-
February 9th, 2012, 12:15 AM
#1
Problem with multi thread application
Hello All,
First thing I have to say is that I am do not know much about implementing multiple threads in an application.
Having said that here is my problem:
I am using RAD Studio C++ builder 2010 and I need to have multiple forms of the same type open, these forms need to poll a device @ a set interval via an ip address using a TidTCPClient, I have tried a number of methods but none so far have been reliable enough.
my Execute method has
std::auto_ptr<TTimer> t(new TTimer(NULL));
t->Interval = 100;
t->OnTimer = &tTimer;
t->Enabled = true;
std::auto_ptr<TTimer> tRead(new TTimer(NULL));
tRead->Interval = interval;
tRead->OnTimer = &tReadTimer;
tRead->Enabled = true;
while(!Terminated)
{
if(terminate)
{
t->Enabled = false;
tRead->Enabled = false;
}
if (MsgWaitForMultipleObjects(0, NULL, FALSE, 800, QS_ALLINPUT) == WAIT_OBJECT_0)
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
my timer event has
if(Terminated)
{
terminate = true;
return;
}
ConnectTCPClient();
UnicodeString s;
values = NULL;
if(!offLine)
{
try
{
try
{
tcpWorker->IOHandler->WriteLn(device->requestString); //send the request
values = tcpWorker->IOHandler->ReadLn();
//should end request go here
if(!tcpWorker->IOHandler->ReadLnTimedout && read)
{
tcpWorker->IOHandler->WriteLn(device->endRequest); //send the end data request
Synchronize(&updateParent);
read = false;
}
else
offLine = true;
}
catch(EIdConnClosedGracefully & cg)
{
//tcpWorker->Disconnect();
}
catch(EIdException & eid)
{
s = eid.Message;
}
}
__finally
{
if(tcpWorker->Connected())
tcpWorker->Disconnect();
}
}
each form creates it's own thread and I think that it is here that the problem begins, I am not sure if I should have one thread or one for each running form
the above code seems to work to a point but tends to lock up randomly, this may be due to connection timeout with th TidTCPClient or something else.
I have tried to use a TCriticalSection and its Acquire() and Release() methods but did not help me.
When I tried using a single thread, I checked for the signalled state of the owner form but this caused more problems.
I have seached High and Low for real world examples but found nothing for my needs so am asking if anyone can help out here.
Cheers
-
February 10th, 2012, 09:14 AM
#2
Re: Problem with multi thread application
 Originally Posted by little john
Hello All,
First thing I have to say is that I am do not know much about implementing multiple threads in an application.
And this is the problem.
You can't just write code that is supposedly multithreaded and not know about threading and various issues, inside-out. Sorry, but that's how it is with multithreaded programming.
Unless you slowly gain experience knowing exactly what synchronization objects to use, when to use them, and how to diagnose any errors using them, then forget about it. Either get someone on your end who knows multiple threads, or take the time to learn the basics first. This requires you to slowly familiarize yourself with these topics. You can't learn these things by just throwing stuff on the wall and hoping something sticks.
Also, please use code tags when posting code, as your code is unreadable without them.
Just this line alone brings up issues. When is this variable set? Who sets it? Is setting this variable synchronized correctly?
You really need someone on your end that knows these issues, as posting snippets of code is not going to help. Snippets do not convey the flow of the program.
(Just to let you know, there are companies that will absolutely refuse to hire programmers who have no multithreaded experience, regardless of how much other programming experience they may have. That shows how in-depth this topic is).
Regards,
Paul McKenzie
Last edited by Paul McKenzie; February 10th, 2012 at 09:18 AM.
-
February 10th, 2012, 05:13 PM
#3
Re: Problem with multi thread application
 Originally Posted by little john
Hello All,
First thing I have to say is that I am do not know much about implementing multiple threads in an application.
I have found this to be a very helpful read: http://www.hpl.hp.com/personal/Hans_...eadsintro.html
Many other useful links to be found in this thread as well: http://www.codeguru.com/forum/showthread.php?t=483584
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|