Solved

Thread Performance Problems

Posted on 2001-06-14
11
156 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

808 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