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
How to End a Thread?
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.
Regards,
John.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
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)
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)