Page 1 of 1
How to End a Thread?
Posted: Wed Jun 20, 2007 4:22 pm
by astrocrep
So if I have a couple of threads running, how do I handle whatever happens when the thread comes to a completion.
Do I need have every thread call a _texit(0); or something similar to signal the scheduler to remove this thread from the queue? Or is there some kind of interrupt fired when a thread runs out of its code space.
Thanks,
Rich
Posted: Wed Jun 20, 2007 4:42 pm
by jnc100
You could do it the same way you end a process. For example, if you have a main(int argc, char **argv) function, when creating the thread you'd probably create its stack containing argv, argc, return address, and then iret to the start of the function, which would set up esp three items into the stack. Execution would then continue normally until you reach the end of main(), which gcc would encode as 'ret' which would return to the return address you set up. You could make this return address the start of your thread_exit function, and then it that test if this is the last thread of the process and if so call a process exit function.
Regards,
John.
Posted: Thu Jun 21, 2007 6:32 am
by Combuster
Alternatively, you could write a function like the following:
void run_thread
{
code = get_function_pointer();
code();
exit_thread();
}
and run that everytime you create a new thread. Regardless of the method, ending a thread will always need a call to kernel space, so you won't be able to get rid of the corresponding system call. That is, unless you are serious on causing a page fault instead which can be best described as a Bad Thing(tm)
Posted: Thu Jun 21, 2007 10:38 am
by Crazed123
That function won't work, because the kernel cannot (and should not) directly call user-space code.
Posted: Thu Jun 21, 2007 12:03 pm
by bluecode
Provide a different thread entry point in your library, which calls the real thread entry and after that the syscall.
Posted: Thu Jun 21, 2007 4:38 pm
by Combuster
Crazed123 wrote:That function won't work, because the kernel cannot (and should not) directly call user-space code.
Its meant to be run in userspace, obviously