ct_exit

The ct_exit function notifies the Cheap Threads scheduler that the current thread should not run again.

Prototype:

int ct_exit( void );

Parameters:

None.

Return Value:

CT_OKAY if successful, or CT_ERROR if not. Currently the only way to receive an error from ct_exit() is to call it when the scheduler is not running.

Discussion:

When a thread calls ct_exit(), the scheduler marks the thread for destruction. Later, when the thread returns control, the scheduler calls the thread's cleanup function, if there is one, and destroys the thread. Other threads are not affected, and the scheduler continues to run.

If called from a user exit, ct_exit() applies to the thread associated with the user exit.

Note that ct_exit() itself does not return control to the scheduler. The thread must still return control by a return statement in the usual way.

When the scheduler destroys a thread, it also cleans up any associated data structures used within Cheap Threads. In particular it discards any pending messages for that thread, and unsubscribes the thread from any message types to which it may have subscribed.

However, the scheduler does not clean up any application data associated with the thread. It is the responsibility of the application to do so as needed, typically by installing a cleanup function.

Restrictions:

It makes sense to call ct_exit() only from a thread or user exit, i.e. when the scheduler is running. If you call ct_fatal_error() when the scheduler is not running, it returns CT_ERROR, and puts the scheduler into an error state which you can clear only by calling ct_clear().


Home