• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

Thread Performance Problems

I have a Communications application in which I am having 5 threads running in it,

1) Application Thread
2) To receive data from serial port
3) To validate the packets and route to Data link layer
4) Datalink layer functionality(seq_nos,retrys,etc)
5) Routing the receved messages to proper windows.

In the above threads normally all the threads will be in waiting mode waiting for some data to arrive on their receive queues.

This is degrading the application performance and it is becoming very slow.

Can any expert suggest some efficient way?
0
micexpert
Asked:
micexpert
  • 3
  • 2
  • 2
  • +4
2 Solutions
 
snoeglerCommented:
>> in waiting mode

What waiting mode? How do you wait?
Do you know about the topics
- overlapped I/O
- events / WaitForXXX()
- message loops?
0
 
makerpCommented:
you should let the OS block for you when ever possible, for example when calling a Recv on a socket it is best to call it in blocking mode rather than poll it every so often. the OS will wake the thread up (set it to the ready state) when the event happens that it is wating for. i this case when a packet comes in.

Paul
0
 
jhanceCommented:
I have trouble believing that 4 or 5 waiting thread are seriously impacting your application performance.  There is something else going on here.  Are the threads REALLY waiting (like in WaitForSingleObject) or are they polling, like:

while(bFlag != TRUE){
  Sleep(100);
}

of similar?

Some additional information about what state these threads are in would probably shed some light on this problem.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
makerpCommented:
jhance, my thoughts are the same about this, i can not see 4-5 threads causing problems if they are waiting for things to happen which is the case for the last 4 threads metioned in the Q
0
 
ekcCommented:
Could you post your "waiting" code of all the threads, it is obvious that all of us are thinking pretty similar...
0
 
jhanceCommented:
micexpert,

WHERE ARE YOU??
0
 
micexpertAuthor Commented:
Dear jhance,

I am waiting continuously in a while loop checking the Receive Queue of the thread for any data arrival and in the loop I am using the Message Dispatching mechanism(i.e, PeekMessage and Pumpmessage).
0
 
pcj0961Commented:
I have an application that uses several threads to receive and process RS232 data.  I had a  problem with the application (windows GUI) performance and inserting the PeekMessage call into every thread (worker threads in my case)  solved the problem.   just make sure that any loop in the threads hits this call.   The other thing to look at is what priority the threads are being set at.

// retrieve messages from message queue while looping in the thread
if(PeekMessage( &Queue, NULL, MTE_BASE, MTE_BASE+LAST_MSG, PM_REMOVE ) == TRUE)
{
}
0
 
jhanceCommented:
>>I am waiting continuously in a while loop checking the Receive Queue

OK, that explains your performance problem!  For every thread you have waiting in a while loop, your app performance is divided by the number of threads running.  This is INCREDIBLY BAD programming design on any operating system that provides an alternative to looping.  Windows provides several alternatives.

As a minimum, you could put a timer in your app and only poll when the timer goes off.  At least during the intervals between timer events your app is free to work without the threads sucking the CPU time.

A much better approach would be to use the notifications provided by the operating system to tell your app or threads that something they are interested in has happened and they should wake up and do something about it.  

At your lowest level doing serial input, the Windows ReadFile() API has the OVERLAPPED structure as an option.  This gives you the ability to setup ReadFile to "go away and do its work" and your app will receive a message when it's done or when an error happens.  

When communicating between the different levels of your application, each can use a similar technique to signal the other level that something has changed.  In a correctly designed scheme, all of your threads should be quitely waiting in something like a WaitForSingleObject() call until the next event has happened.
0
 
micexpertAuthor Commented:
Thanks jhance,

I don't agree with you in using the timers for polling. I have got an idea while seeing your comment, I will suspend the thread instead of polling for data arrival and whenever somebody wants to write data into the thread's queue he will do so by first resuming the thread and writing in to queue. After the processing of data is completed the thread will suspend automatically.

Is it a good way..
0
 
Roshan DavisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : makerp, jhance (points to be split)

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now