Threads and Semaphores: 4.1 Using Multiple Threads: How GEOS Threads Are Created

Up: GEOS SDK TechDocs | Up | Prev: 4 Using Multiple Threads | Next: 4.2 Managing Priority Values

GEOS threads can be created in three different ways. The first thread (or pair of threads, in the dual-thread architecture) for each application is created automatically when the application is launched. By calling the appropriate routines, the application can create additional threads to handle messages sent to certain objects or to run procedural code.

The Application's Primary Thread

The application's primary thread is created automatically by GEOS when the application is launched. (See the Applications and Geodes chapterfor information on launching applications.) For example, if a user double-clicks on your application's icon in a GeoManager window, GeoManager calls the library routine UserLoadApplication() , specifying the geode file and certain other parameters. This calls the GeodeLoad() routine in the GEOS kernel.

If the program is written using the single-thread model, GEOS creates an event-driven thread to handle messages sent to any object in the program. If the program is written using the dual-thread model, GEOS creates one event-driven thread to handle messages sent to the program's user interface objects and another to handle messages sent to other objects in the program.

If the program requires more than two threads, the extra thread(s) must be allocated manually on startup and destroyed before the application exits completely.

Event-Driven Threads

ThreadAttachToQueue()

To create an event-driven thread (one that handles messages sent to certain objects), send a MSG_PROCESS_CREATE_EVENT_THREAD to your application's primary thread, passing as arguments the object class for the new thread (a sublass of ProcessClass ) and the stack size for the new thread (1 K bytes is usually a good value, or around 3 K bytes for threads that will handle keyboard navigation or manage a text object). This message is detailed in the Process Object chapter.

GEOS will create the new thread, give it an event queue, and send it a MSG_META_ATTACH . Initially, the thread will handle only messages sent to the thread itself. If the thread creates any new objects, however, it will handle messages sent to those objects as well. To control the behavior of the new thread, define a subclass of ProcessClass and a new handler for MSG_META_ATTACH . The new handler can create objects or perform whatever task is needed. Be sure to start your new handler with @callsuper() so that the predefined initializations are done as well.

If you have a thread that you want attached to a different event queue, you can use ThreadAttachToQueue() . This routine is not widely used except when applications are shutting down and objects need to continue handling messages while not returning anything. It's unlikely you will ever use this routine.

Threads That Run Procedural Code

ThreadCreate()

To create a thread to run procedural code, first load the initial function into fixed memory. Then call the system routine ThreadCreate() , passing the following arguments: The base priority for the new thread, an optional sixteen-bit argument to pass to the new thread, the entry point for the code, the amount of stack space GEOS should allocate for the new thread, and the owner of the new thread.


Up: GEOS SDK TechDocs | Up | Prev: 4 Using Multiple Threads | Next: 4.2 Managing Priority Values