Link to home
Start Free TrialLog in
Avatar of l_madhavi
l_madhavi

asked on

Alternate to while loop in threads

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
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial