Solved

Thread Performance Problems

Posted on 2001-06-14
11
154 Views
Last Modified: 2013-11-20
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
Comment
Question by:micexpert
  • 3
  • 2
  • 2
  • +4
11 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 6194244
>> in waiting mode

What waiting mode? How do you wait?
Do you know about the topics
- overlapped I/O
- events / WaitForXXX()
- message loops?
0
 
LVL 10

Accepted Solution

by:
makerp earned 25 total points
ID: 6194621
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
 
LVL 32

Assisted Solution

by:jhance
jhance earned 25 total points
ID: 6195444
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
 
LVL 10

Expert Comment

by:makerp
ID: 6195521
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
 
LVL 5

Expert Comment

by:ekc
ID: 6196400
Could you post your "waiting" code of all the threads, it is obvious that all of us are thinking pretty similar...
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 32

Expert Comment

by:jhance
ID: 6196498
micexpert,

WHERE ARE YOU??
0
 

Author Comment

by:micexpert
ID: 6197505
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
 
LVL 1

Expert Comment

by:pcj0961
ID: 6197798
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
 
LVL 32

Expert Comment

by:jhance
ID: 6197868
>>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
 

Author Comment

by:micexpert
ID: 6199347
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9484708
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

21 Experts available now in Live!

Get 1:1 Help Now