systhread, ReadFile and cpu usage

Nov 13, 2006 at 12:01am

systhread, ReadFile and cpu usage

Hi,

I’m using a systhread in my external. In the thread i have an infinite loop calling ReadFile, to call from a USB device. As soon as I add the external to a patch, I can see max using around 94% CPU even though the thread is not necessarily doing anything (e.g. ReadFile unsuccesful).

I’m on Windows XP SP2 Pro and MAX/MSP 4.5.7. Any ideas? Is it just a matter of thread priority?

Thanks,
Bert.

#28665
Nov 13, 2006 at 7:24pm

On Nov 12, 2006, at 4:01 PM, bert wrote:

> I’m using a systhread in my external. In the thread i have an
> infinite loop calling ReadFile, to call from a USB device. As soon
> as I add the external to a patch, I can see max using around 94%
> CPU even though the thread is not necessarily doing anything (e.g.
> ReadFile unsuccesful).

As is typical in multi-threaded programming using infinite loops, you
probably want your thread to sleep periodically (systhread_sleep) or
some other means of thread synchronization with your main thread
(e.g. condition variables, semaphores, etc.). Otherwise the thread
will consume 100% CPU. For example the following code, even though it
“does nothing”, will use 100% CPU:

while (true) {
;
}

If this is confusing, I would highly recommend reading some
literature on multi-threaded programming. One text book I like is
“Modern Operating Systems” by Andrew Tanenbaum, but there are also
lots of articles on the subject online.

-Joshua

#88293

You must be logged in to reply to this topic.