[comp.sys.next] How do you use threads from an objective-c program?

roy@prism.gatech.EDU (Roy Mongiovi) (04/03/89)

As a recent owner of a NeXT machine and having just received my copy
of the technical doc, I wanted to write a simple program to use the
interface builder and display postscript to do some graphics on the
beast.

I rewrote a simple program I've played with since I first learned to
program.  It uses vector addition to draw a "spirograph."  Despite
having to learn objective-c and fighting with the idiosyncracies of
the interface builder, I got it working in a couple of days.

Unfortunately, it has one problem.  Once the "draw" method of the
spirograph has been invoked, the application is busy until the
entire spirograph is drawn.  In particular, my "stop" key waits
until much too late to do anything.

Ahah! I thought.  This sounds like a job for threads.  So I hacked
up the code so that the main drawing loop is a separate function
and ran it in another thread.  Imagine my surprise when the window
server complained that the client connection had closed prematurely
and then bombed out.

It's repeatable.  It draws a spirograph apparently up until the
postscript commands get flushed, and then bombs out the window
server.  Is it something I said?  Or am I just doing something
incorrectly?  This seems like the sort of thing an application
might want to do alot of: updating a window while allowing changes
to the application's parameters.

Can someone tell me what I did wrong or what the right way is to
do this?  Just point me in the right direction....  Thanks.
-- 
Roy J. Mongiovi     Systems Support Specialist     Office of Computing Services
Georgia Institute of Technology	  Atlanta, Georgia  30332-0275   (404) 894-4660
	uucp: ...!{allegra,amd,hplabs,ut-ngp}!gatech!prism!roy
	ARPA: roy@prism.gatech.edu

ali@polya.Stanford.EDU (Ali T. Ozer) (04/07/89)

In article <387@hydra.gatech.EDU> roy@prism.gatech.EDU (Roy Mongiovi) writes:
>Ahah! I thought.  This sounds like a job for threads.  So I hacked
>up the code so that the main drawing loop is a separate function
>and ran it in another thread.  Imagine my surprise when the window
>server complained that the client connection had closed prematurely
>and then bombed out.

Yes, it is a job for threads, but no, you cannot do what you tried to do.
Reason? The window server currently cannot talk to two seperate threads at 
the same time within one context. 

One solution is to have the thread do the compute-intensive stuff and have the
main thread do all the PostScript communication. Another solution might be
to create another context; but I haven't had the chance to explore that yet
and am not sure about the details right now...

In the meantime, if you wish to do tons of drawing but you also want
to be able to use your menus, buttons, etc at the same time, look
at timed entries. With a timed entry, a function can be called repeatedly
every so often. Because the timed entries act like events, user events
also fire and can be serviced between calls to the timed entry function.

Ali Ozer, NeXT Developer Support