CreateThread()
function creates a thread:
Item CreateThread ( cconst char *name, uint8 pri, void (*code) (), int32 stacksize )
name
, is the name of the thread to create. The second argument, pri
, is the priority that the thread will have. The third argument, code
, is a pointer to the code that the thread will execute. The last argument, stacksize
, specifies the size in bytes of the thread's stack. If successful, the call returns the item number of the thread. If unsuccessful, it returns an error code.
Before calling CreateThread(),
you must determine what stack size to use for the thread. There is no default size for the stacksize
argument; however, it's important to make the size large enough so that stack overflow errors do not occur. Stack overflow errors are characterized by random crashes that defy logical analysis, so it's a good approach to start with a large stack size and reduce it until a crash occurs, then double the stack size. In the sample code later in this chapter, the stack size is set to 10000. A good size to start with is 2048.
Example 1 shows the process of creating a thread:
Example 1: Starting a task.
#define STACKSIZE (10000)
...
int main(int argc, char *argv[])
{
uint8 Priority;
Item T1;
...
Priority = CURRENTTASK->t.n_Priority;
T1 = CreateThread("Thread1", Priority, Thread1Proc, STACKSIZE);
...
}
int Thread1Proc()
{
/* This is the code for Thread1Proc */
}
The code example given at the end of this chapter is a good illustration of using CreateThread()
to start two threads. It shows the use of global variables that are shared by all threads, which signal among threads and the parent task. Signals for both threads are first allocated with AllocSignal().
The parent task then uses WaitSignal()
to wait for an event from either of the threads.
DeleteThread()
to end a thread when a parent task finishes with it:
Err DeleteThread( Item thread )
exit()
or just returning.
CreateThread()
and DeleteThread()
functions are convenience routines, which make it easy to create and delete threads for the most common uses. It is sometimes necessary to have better control over thread creation. This requires allocating resources yourself and creating the thread item using CreateItem()
or CreateItemVA()
. Creating a thread involves allocating memory for the stack that the thread will use, and constructing a tag argument list that describes how the thread should be created. The tags you supply are:
msg_Result
field of the message contains the exit status of the task. This is the value the task provided to exit()
, or the value returned by the task's primary function. The msg_DataPtr
field of the message contains the item number of the task that just terminated. If a task exited on its own, this is the item number of the task itself. It is the responsibility of the task that receives the status message to delete it when the message is no longer needed by using DeleteMsg().
exit()
; the memory used for its stack must be freed automatically. When this tag is not provided, you are responsible for freeing the stack whenever the thread terminates.