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


Thread Performance Problems

Posted on 2001-06-14
Medium Priority
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?
Question by:micexpert
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
  • 3
  • 2
  • 2
  • +4

Expert Comment

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?
LVL 10

Accepted Solution

makerp earned 100 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.

LVL 32

Assisted Solution

jhance earned 100 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){

of similar?

Some additional information about what state these threads are in would probably shed some light on this problem.
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 10

Expert Comment

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

Expert Comment

ID: 6196400
Could you post your "waiting" code of all the threads, it is obvious that all of us are thinking pretty similar...
LVL 32

Expert Comment

ID: 6196498


Author Comment

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).

Expert Comment

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
LVL 32

Expert Comment

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.

Author Comment

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..
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.


Roshan Davis
EE Cleanup Volunteer

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

721 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