Subj : Adding threads to a doublelinked list To : comp.programming.threads From : JS Date : Tue Apr 19 2005 11:48 am This function creates a thread. I have also made a double-linked list that a thread should be added to when it is created. But I don't know when I should add it to this list. I have tried to add it just after I use malloc but I am not sure that is right. #include #include #include typedef int mythread_t; typedef struct _mythread_attr_t { } mythread_attr_t; #define STACK_SIZE 1024 struct pcb { void *(*start_routine) (void *); void *arg; jmp_buf state; int stack[STACK_SIZE]; }; static struct pcb first_thread; struct pcb *current = &first_thread; struct pcb *ready = NULL; /* Vi har ikke en ventende tråd endnu */ int mythread_create (mythread_t *threadp, const mythread_attr_t *attr, void *(*start_routine) (void *), void *arg) { int *state_pointer; int local_occ; struct pcb *pcb_pointer; pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb)); if(pcb_pointer == NULL) { exit(-1); } pcb_pointer->start_routine = start_routine; pcb_pointer->arg = arg; if(setjmp(pcb_pointer->state)) { current->start_routine(current->arg); printf("Thread has returned - Stop everything\n"); exit(0); } state_pointer = (int *) &pcb_pointer->state; local_occ = state_pointer[JB_BP] - state_pointer[JB_SP]; state_pointer[JB_BP] = (int) (pcb_pointer->stack + STACK_SIZE); state_pointer[JB_SP] = (int) state_pointer[JB_BP] - local_occ; ready = pcb_pointer; *threadp = (int) pcb_pointer; return 0; } .