# Some problems about Thread Control

**URL:** <https://forum.legato.io/t/some-problems-about-thread-control/4126>\
**Category:** Legato Application Framework\
**Created:** [February 19, 2019, 10:05am UTC](https://forum.legato.io/t/some-problems-about-thread-control/4126 "2019-02-19T10:05:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phoneixSH](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/phoneixsh/32/631_2.png) [@phoneixSH](https://forum.legato.io/u/phoneixSH)\
**Post date:** [February 19, 2019, 10:05am UTC](https://forum.legato.io/t/some-problems-about-thread-control/4126/1 "2019-02-19T10:05:39Z")

</div>

Our team is moving code from t-kernel to legato and has two threading problems:

1. le\_thread\_Join in legato can release the resources of sub-threads in the main thread, but I hope that self-termination(use le\_thread\_Cancel) and deletion(Delete thread and release the space occupied by threads) can be done in sub-threads. What should I do in legato Application?

2. There are two sets of functions in t-kernel: tk\_slp\_tsk() and tk\_wup\_tsk (tskid).They can self-suspends threads and other threads wake-up threads at specified locations, because there is no similar interface in legato. I use mutex and cond in Linux to complete this pair of interfaces.(Does using these Linux functions in legato threads cause problems?)

The other pair of interface is tk\_sus\_tsk (tskid) and tk\_rsm\_tsk (tskid), which can suspend and wake up the specified thread by passing in ID(In any thread). This seems not only impossible in legato, but also troublesome in linux. I hope to provide some ideas.

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [February 26, 2019, 1:40pm UTC](https://forum.legato.io/t/some-problems-about-thread-control/4126/2 "2019-02-26T13:40:25Z")

</div>

1. You can directly use those thread api.
2. You can have a try, i believe it is fine.  
[c - Linux Threads suspend/resume - Stack Overflow](https://stackoverflow.com/questions/11468333/linux-threads-suspend-resume)

---

<div class="post-metadata">

**Author:** ![davidc](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@davidc](https://forum.legato.io/u/davidc)\
**Post date:** [February 26, 2019, 10:01pm UTC](https://forum.legato.io/t/some-problems-about-thread-control/4126/3 "2019-02-26T22:01:43Z")

</div>

Hi @phoneixSH,

I’ve been using the Legato thread API for a while now - mostly to run some operations at different RT levels - and haven’t had too much grief. Starting and stopping them from a control thread hasn’t been an issue - although I haven’t had to pass data between the threads (I pass in setup and config data via a context at thread creation).

Two things to note:

1. you will need to call api `xxx_ConnectService()` functions in your thread `main` for any legato APIs to attach and operate correctly (eg, if you want to use the le\_cfg API, you need to call `le_cfg_ConnectService()`). This also goes for any private APIs that you have created yourself.
2. you **MUST** have a call to `le_event_RunLoop()` at the end of your thread `main` function - or none of the legato events (timers, dfMonitors etc) will occur.

ciao, Dave

---

<div class="post-metadata">

**Author:** ![phoneixSH](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/phoneixsh/32/631_2.png) [@phoneixSH](https://forum.legato.io/u/phoneixSH)\
**Post date:** [February 28, 2019, 2:49am UTC](https://forum.legato.io/t/some-problems-about-thread-control/4126/4 "2019-02-28T02:49:31Z")

</div>

Perhaps `pthread_kill(...SIGSTOP)`can suspend and resume other thread unconditionally.  
I will try it ，Thank you very much。

---

<div class="post-metadata">

**Author:** ![phoneixSH](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/phoneixsh/32/631_2.png) [@phoneixSH](https://forum.legato.io/u/phoneixSH)\
**Post date:** [February 28, 2019, 3:22am UTC](https://forum.legato.io/t/some-problems-about-thread-control/4126/5 "2019-02-28T03:22:49Z")

</div>

Thank you for your answer。  
Question 1 has been solved。  
Question 2 My description may not be very clear.  
Unfortunately, in question2 ,I can’t change the logic code in any thread function. All I can do is to replace and modify the interface of the T-kernel operating system with the API of legato.([www.t-engine.org has moved](http://www.t-engine.org/ja/wp-content/themes/wp.vicuna/html/t-kernel_2.0/html/reference.html#list_of_c_language_interface))  
For example:  
ER tk\_cre\_tsk(const T\_CTSK\_SYSCALL \*pk\_ctsk) {  
mTaskCtrl[taskIDCount-1].thd\_ref = le\_thread\_Create(idStr, pk\_ctsk-\>task, NULL);  
le\_thread\_SetPriority(mTaskCtrl[taskIDCount-1].thd\_ref, (pk\_ctsk-\>itskpri)+LE\_THREAD\_PRIORITY\_HIGH);  
le\_thread\_SetStackSize(mTaskCtrl[taskIDCount-1].thd\_ref, pk\_ctsk-\>stksz);  
}

When the upper logic calls this interface, the thread will executed the function pointer (pk\_ctsk-\>task). I can’t predict and change the contents of this function.

So, if i want to suspend and resume other thread unconditionally, I can’t rely on the conditional variables(pthread\_cond) shared by threads.

Because we can’t find a solution for the time being, we have only completed the modifications of tk\_slp\_tsk（suspend current thread） and tk\_wup\_tsk（wakeup other suspended thread）, while the interface between tk\_sus\_tsk（suspend other thread） and tk\_rsm\_tsk（wakeup other suspended thread） has been abandoned temporarily.

Thank you for your answer anyway. Question 2 has been thrown to other groups for investigation.
