Is there an NSThread equivalent of suspend/resume?

Duncan Champney duncanc at aol.com
Sat May 5 10:27:01 PDT 2007


My application uses a very simple form of multithreading in order to  
detach multiple rendering threads for rendering fractal images. I  
create an array of rendering "tasks", and then detach as many  
rendering threads as there are processors on the target machine. (I  
don't mean "tasks" in the sense of NSTask instances. I'm using the  
term informally to describe a unit of work for my application to  
perform.)

When a thread completes, It shuts down. My main thread then takes  
another rendering task from the task array and detaches a new  
rendering thread to compute that "task." Thus, for a plot that has a  
large number of rendering tasks, my application detaches and releases  
rendering threads repeatedly. There is a fair amount of overhead in  
detaching and releasing threads. I would like to avoid that overhead.  
I also am adding a feature to my app that will require allocating  
large memory structures for each rendering thread. I will allocate a  
memory structure for each rendering thread, and want that structure  
to be used to process all the rendering tasks in my task array.

I want to re-factor my code so that instead of releasing my rendering  
threads once they finish a rendering task, they go to sleep or are  
suspended until they are given another rendering task.

My main thread would then release them once the plot is completed.

I want to minimize/eliminate the amount of CPU time that idle compute  
threads take, while reducing the amount of latency before a rendering  
thread wakes up and begins a new rendering task.

I could add nanosleep calls to a rendering thread's run loop, so that  
it sleeps for brief intervals before checking for a new rendering  
task. However this approach would mean that the rendering task might  
take as long as the full sleep interval before it "wakes up" and  
begins processing it's new rendering task. If I used too large an  
interval, I would introduce lag time before a thread wakes up and  
begin processing a new task. If I use too short an interval, my  
threads would waste CPU time needlessly as they go to sleep, wake up,  
find that there is nothing to do, and go back to sleep again. The  
ideal sleep interval would probably be different for different  
machines, and might even vary depending on what else the machine was  
doing.

Ideally, I'd like to have my main thread suspend a rendering thread  
after it completes, and then resume it once it has queued up a new  
rendering task. I couldn't find support for this in the documentation  
for threads. Is there an equivalent of suspend/resume calls for  
threads as opposed to NSTasks? If so, what are the calls and where  
are they documented? Some sort of "wake early" call would also solve  
my problem (a call, performed from the main thread, that would wake a  
rendering thread from sleep before its sleep interval had elapsed)



Duncan C




More information about the MacOSX-dev mailing list