Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Technology Partners: 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!

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

578 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