Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Alternate to while loop in threads

Posted on 2004-09-25
1
Medium Priority
?
217 Views
Last Modified: 2013-11-20
Hello,

 

I am new to Mulithreading. In my project I am working with multiple threads. Since I haven’t worked with threads before, I am facing many hurdles. Can any one please guide me or give some hints.

 I am explaining in brief about the threads used by me in my project.

 

1st thread:  Filter Thread

Is started at application start up.

Filters each line read from file( Log data is read from file)

I have used CCriticalSection and CSingleLock to lock the data access here as this

LIST is accessed in Display thread.

Adds the data to the LIST from top.

This thread has to run continuously till the application end.

 

2nd thread:Display Thread

Is started at application start up.

Displays each line filtered by Filter thread

I have used CCriticalSection and CSingleLock to lock the data access here as this

LIST is accessed in Filter thread.

Retrieves the data from the LIST from bottom.

Deletes the data from the list after display.

This thread has to run continuously till the application end.

 

3rd Thread: Monitor Thread      

Is started at application start up.

This thread has to continuously check if new records are added to the file ( Here another external application will add records to the file)

If new records are added, it notifies the Filter Thread, which fetches the added records, filters them and gives to Display thread for displaying.

Since all the above three threads have to run continuously, I had used sleep in all the threads as shown below.

UINT ThreadFunction(LPVOID lp)
{
while(TRUE)
{

     // Do the work
}
}

 

The result of this is that CPU time is 100% used because of the While loop.  

 
Can anyone please suggest an alternative for while loop in the other two threads.


Thanks
Madhavi
0
Comment
Question by:l_madhavi
[X]
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
1 Comment
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 120 total points
ID: 12155665
UINT ThreadFunction(LPVOID lp)
{
bool bContinue;
while(TRUE)
{
bContinue = true;
  while(bContinue)
{
//Check for new line(s) file  if none found set bContinue to false to fall through to outer loop
}
     sleep(1000);  //set thread into sleep state for 1 second
}
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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 …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

604 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