[PLUG] high cpu utilization
devendralaulkar at yahoo.com
Wed Nov 11 10:33:37 IST 2009
>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"
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.
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 .
More information about the Plug-mail