# Thread issues

**URL:** <https://forums.digi.com/t/thread-issues/5269>\
**Category:** NET+OS\
**Created:** [September 3, 2009, 1:34pm UTC](https://forums.digi.com/t/thread-issues/5269 "2009-09-03T13:34:43Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamp](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@adamp](https://forums.digi.com/u/adamp)\
**Post date:** [September 3, 2009, 1:34pm UTC](https://forums.digi.com/t/thread-issues/5269/1 "2009-09-03T13:34:43Z")

</div>

I have two threads created with tx\_thread\_create()

both with the following general options:

```
void * stack_ptr = NULL;
unsigned int status;
TX_THREAD reset_settings_thread;
char * threadName = "Reset Settings Thread";

stack_ptr = malloc (8192);
if (stack_ptr == NULL) {
printf ("Unable to allocate thread stack for thread.

```

");  
return -1;  
}

```
status = tx_thread_create(&amp;reset_settings_thread, /* control block for thread*/
							threadName, /* thread name*/
							(entry_functionType)reset_settings,	/* entry function*/
							0, /* parameter*/
							stack_ptr, /* start of stack*/
							8192, /* size of stack*/
							BSP_MEDIUM_PRIORITY, /* priority*/
							BSP_MEDIUM_PRIORITY, /* preemption threshold */
							1, /* time slice threshold*/
							TX_AUTO_START); /* start immediately*/

```

The only differences with them are the control block, thread name and entry function.

The problem is that I cannot run both of them at the same time, only one works at a time.

Anyone have any ideas?

Thanks

---

<div class="post-metadata">

**Author:** ![egawtry](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@egawtry](https://forums.digi.com/u/egawtry)\
**Post date:** [September 3, 2009, 3:10pm UTC](https://forums.digi.com/t/thread-issues/5269/2 "2009-09-03T15:10:23Z")

</div>

Two immediately noticable things:

1. You need to do a memset() to 0 on the TX\_THREAD before use.
2. Standard is to use 10 for the timeslice, not 1.

#2 might be slowing the thread way down so it appears not to be running.

-Erik

---

<div class="post-metadata">

**Author:** ![sparkys\_dad](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@sparkys\_dad](https://forums.digi.com/u/sparkys_dad)\
**Post date:** [September 3, 2009, 1:41pm UTC](https://forums.digi.com/t/thread-issues/5269/3 "2009-09-03T13:41:06Z")

</div>

Hello

A couple of questions.

can you define: “only one works at a time”  
Do you mean that 1) you can only create one thread at a time or do you mean 2) you believe that only one runs at a time?

If 1) can you supply the error that tx\_thread\_create returns?  
If 2) how do you know that only one is running?

I have run applications with any number of threads so I know that multiple threads can simultaneously run.

I use this technique for, among other things a performance monitor thread that might show the relative amount of memory used or cpu utilization used while another thread is doing real work.

---

<div class="post-metadata">

**Author:** ![adamp](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@adamp](https://forums.digi.com/u/adamp)\
**Post date:** [September 4, 2009, 8:16am UTC](https://forums.digi.com/t/thread-issues/5269/4 "2009-09-04T08:16:30Z")

</div>

I think that only one thread appears to run at one time.

I can create both threads and tx\_thread\_create() returns TX\_SUCCESS for each, so I know that they have been properly created.

Its the functions that have been called that dont appear to work. For testing purposes I have basic printf statements in each and when both threads are started, both functions are not executed.

---

<div class="post-metadata">

**Author:** ![adamp](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@adamp](https://forums.digi.com/u/adamp)\
**Post date:** [September 4, 2009, 8:57am UTC](https://forums.digi.com/t/thread-issues/5269/5 "2009-09-04T08:57:47Z")

</div>

I’ve looked a little more into the thread situation:

When either of the two threads are run individually the chip is able to run ok but when they are run at the same time it appears that the chip locks up; all other services I had running on it (Web and Telnet server) stop working and it doesnt even respond to ping anymore.

---

<div class="post-metadata">

**Author:** ![adamp](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@adamp](https://forums.digi.com/u/adamp)\
**Post date:** [September 3, 2009, 3:57pm UTC](https://forums.digi.com/t/thread-issues/5269/6 "2009-09-03T15:57:55Z")

</div>

Have done that now, doesnt appear to change it

---

<div class="post-metadata">

**Author:** ![sparkys\_dad](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@sparkys\_dad](https://forums.digi.com/u/sparkys_dad)\
**Post date:** [September 4, 2009, 12:08pm UTC](https://forums.digi.com/t/thread-issues/5269/7 "2009-09-04T12:08:12Z")

</div>

The only other things I can think to ask, and these are long shots, are you using separate TX\_THREAD objects and separate stacks for each thread (that is mallocing space twice, once for thread 1 and one for thread 2 and declaring two TX\_THREAD\_OBJECTS one for thread 1 and one for thread2 )? Also, even more of a long shot, I generally memset the stack space to ‘\0’ before using it.

---

<div class="post-metadata">

**Author:** ![egawtry](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@egawtry](https://forums.digi.com/u/egawtry)\
**Post date:** [September 4, 2009, 2:39pm UTC](https://forums.digi.com/t/thread-issues/5269/8 "2009-09-04T14:39:09Z")

</div>

What are you doing in the threads? If you have a infinate loop without doing a tx\_thread\_suspend(), that could dog down everything else.

---

<div class="post-metadata">

**Author:** ![adamp](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@adamp](https://forums.digi.com/u/adamp)\
**Post date:** [September 4, 2009, 3:18pm UTC](https://forums.digi.com/t/thread-issues/5269/9 "2009-09-04T15:18:12Z")

</div>

Afraid I’ve tried both of those and it doesn’t seem to work.

---

<div class="post-metadata">

**Author:** ![adamp](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@adamp](https://forums.digi.com/u/adamp)\
**Post date:** [September 4, 2009, 3:02pm UTC](https://forums.digi.com/t/thread-issues/5269/10 "2009-09-04T15:02:02Z")

</div>

I do have an infinite loop in both threads but in each of them they run tx\_thread\_sleep() with a parameter of at least 200 timer ticks, so I would think that that would be enough to let the other thread get some time.

---

<div class="post-metadata">

**Author:** ![egawtry](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@egawtry](https://forums.digi.com/u/egawtry)\
**Post date:** [September 5, 2009, 2:50pm UTC](https://forums.digi.com/t/thread-issues/5269/11 "2009-09-05T14:50:24Z")

</div>

Do you keep the TX\_THREAD in scope? If you are calling this in a subroutine, then the TX\_THREAD will go out of scope.

---

<div class="post-metadata">

**Author:** ![adamp](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@adamp](https://forums.digi.com/u/adamp)\
**Post date:** [September 10, 2009, 7:58am UTC](https://forums.digi.com/t/thread-issues/5269/12 "2009-09-10T07:58:10Z")

</div>

No TX\_THREAD is not in scope but I thought that this wasn’t needed when using tx\_thread\_sleep();

tx\_thread\_sleep() only has a parameter of timer ticks and does not need the thread pointer to be in scope.

---

<div class="post-metadata">

**Author:** ![egawtry](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@egawtry](https://forums.digi.com/u/egawtry)\
**Post date:** [September 10, 2009, 2:04pm UTC](https://forums.digi.com/t/thread-issues/5269/13 "2009-09-10T14:04:59Z")

</div>

TX\_THREAD is a buffer that is used by the thread to store its information. If it gets wiped out, then the thread dies.

Try mallocing the TX\_THREAD along with the stack.

---

<div class="post-metadata">

**Author:** ![adamp](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@adamp](https://forums.digi.com/u/adamp)\
**Post date:** [September 10, 2009, 3:52pm UTC](https://forums.digi.com/t/thread-issues/5269/14 "2009-09-10T15:52:52Z")

</div>

Tried mallocing the TX\_THREAD but couldnt get the correct syntax. 😛 In the end I just made TX\_THREAD global and that fixed the problem.

Thanks for your help
