How to End a Thread?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
astrocrep
Member
Member
Posts: 127
Joined: Sat Apr 21, 2007 7:21 pm

How to End a Thread?

Post 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
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post 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.
User avatar
Combuster
Member
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:

Post 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)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Crazed123
Member
Member
Posts: 248
Joined: Thu Oct 21, 2004 11:00 pm

Post by Crazed123 »

That function won't work, because the kernel cannot (and should not) directly call user-space code.
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post by bluecode »

Provide a different thread entry point in your library, which calls the real thread entry and after that the syscall.
User avatar
Combuster
Member
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:

Post 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 :roll:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply