[PLUG] high cpu utilization

Devendra Laulkar devendralaulkar at yahoo.com
Tue Nov 10 21:03:37 PST 2009


Hi,

>In my C program, I have two threads,
> thd_r reading video realtime from an hardware 
> and  thd_w writing to a file.
> thd_r is very slow (as it real time video capture) as
> compared to thd_w. To
> push data from one thread to another  push a buffer in
> a g_queue which get
> pulled in the thd_w and used. But since thd_r is much
> slower then thd_w,  I
> have to wait in thd_w again and again in a while loop
> polling for the data
> to be pushed queue from thd_r. Because of this polling
> using while, huge CPU
> is getting consumed.

So don't poll :) What you are doing is essentially "Busy Waiting"[1]
The writer thread should go to sleep when there are no items in the buffer, and wake up when the reader starts placing items in the buffer.
Your problem is essentially a Producer Consumer problem[2].
You can use semaphores(among other things) to solve this problem. 
A useful site with tutorials on using semaphores and other IPC primitives among other things is at [3].

-Devendra.

[1] http://en.wikipedia.org/wiki/Busy_waiting
[2] http://en.wikipedia.org/wiki/Producer-consumer_problem
[3] http://users.actcom.co.il/%7Echoo/lupg/tutorials/


      



More information about the plug-mail mailing list