Hi, I am developing a system that needs to start a thread at runtime (on external command) and to be able to terminate it regardless of its current state (on another external command or other event - a fault signal from a monitoring thread for example). The thread being spawned must be strictly linear in nature and is incapable of periodically calling chThdShouldTerminate(). It is considered safe to 'kill' the thread at any point of its execution. The thread that spawns the linear thread must continue operation (i.e. must not block to wait for the spawned thread to finish).
It should be possible to spawn the linear thread again from its beginning.
Is there a clean way to implement this functionality (without changing the kernel code)? If not, what would be the proper way to do it? I guess performing the operations in chThdExitS() on the spawned thread from the 'monitoring' thread, followed by a call to chThdWait() would do the job, but there might be a better way.
Any help would be highly appreciated!
Thank you in advance!
Terminating a thread from another thread - is it possible?
Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess
- Giovanni
- Site Admin
- Posts: 14455
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Re: Terminating a thread from another thread - is it possibl
Hi,
Killing a thread is not possible, it is a design choice because it would be inherently not safe in all possible situations.
What you can do is to atomically set the termination flag and reset any "wait" primitive used by the thread. For example, assuming the thread uses a semaphore you can do:
From the monitor thread:
From the linear thread, replace the call to the semaphore wait with:
If you use messages or mailboxes then you could use a kill message instead of chSemResetI();
Using this method the thread would make a termination check each time it attempts to go in a wait state.
Giovanni
Killing a thread is not possible, it is a design choice because it would be inherently not safe in all possible situations.
What you can do is to atomically set the termination flag and reset any "wait" primitive used by the thread. For example, assuming the thread uses a semaphore you can do:
From the monitor thread:
Code: Select all
chSysLock;
tp->p_flags |= THD_TERMINATE;
chSemResetI(sp);
chSchRescheduleS();
chSysUnlock();
chThdWait(tp);
From the linear thread, replace the call to the semaphore wait with:
Code: Select all
msg_t msg;
chSysLock();
if (chThdShouldTerminate())
chThdExitS(RDY_RESET);
msg = chSemWaitS(sp);
if (msg == RDY_RESET)
chThdExitS(msg);
chSysUnlock();
If you use messages or mailboxes then you could use a kill message instead of chSemResetI();
Using this method the thread would make a termination check each time it attempts to go in a wait state.
Giovanni
Re: Terminating a thread from another thread - is it possibl
I got it.
Thank you so much for the great job you've done with ChibiOS!
Wish you all the best,
Ivan
Thank you so much for the great job you've done with ChibiOS!
Wish you all the best,
Ivan
-
- Posts: 26
- Joined: Wed Feb 25, 2015 9:45 pm
- Has thanked: 1 time
- Been thanked: 2 times
Re: Terminating a thread from another thread - is it possible?
Hi,
I know this is an old thread, and chibios has moved on a bit since, but I was wondering how one could reuse the stack of a thread that has exited.
Is the code below code safe to use?
monitoring thread:
Thread to be terminated:
I know this is an old thread, and chibios has moved on a bit since, but I was wondering how one could reuse the stack of a thread that has exited.
Is the code below code safe to use?
monitoring thread:
Code: Select all
chSysLock();
pThd->flags |= CH_FLAG_TERMINATE;
chSemResetI(pSemaphore, 0);
chSysUnlock();
while(!chThdTerminatedX(pThd))
{
chThdYield();
}
/* So that the stack can be reused by another thread */
pThd->wabase = nullptr;
Thread to be terminated:
Code: Select all
msg_t msg;
chSysLock();
if (chThdShouldTerminateX())
chThdExitS(MSG_RESET);
msg = chSemWaitS(pSemaphore);
if (msg == MSG_RESET)
chThdExitS(msg);
chSysUnlock();
- Giovanni
- Site Admin
- Posts: 14455
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Re: Terminating a thread from another thread - is it possible?
Hi,
Stacks can be reused, follow this pattern for termination:
Thread (threadp is the pointer to this thread):
Other thread:
Note, performing chThdWait() is important or the thread would be stuck in the registry, reusing the stack would corrupt it.
Giovanni
Stacks can be reused, follow this pattern for termination:
Thread (threadp is the pointer to this thread):
Code: Select all
while (!chThdShouldTerminateX()) {
/* Waiting for something.*/
msg = chSemWait(&sem);
if (msg != MSG_RESET) {
/* Do things */
}
}
return;
Other thread:
Code: Select all
chSemReset(&sem);
chThdWait(threadp);
/* Now stack can be reused.*/
Note, performing chThdWait() is important or the thread would be stuck in the registry, reusing the stack would corrupt it.
Giovanni
-
- Posts: 26
- Joined: Wed Feb 25, 2015 9:45 pm
- Has thanked: 1 time
- Been thanked: 2 times
Who is online
Users browsing this forum: No registered users and 37 guests