Thread issues

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(&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?


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.



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.

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.

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.

Have done that now, doesnt appear to change it

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.

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.

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

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.

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.

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.

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.

Tried mallocing the TX_THREAD but couldnt get the correct syntax. :stuck_out_tongue: In the end I just made TX_THREAD global and that fixed the problem.

Thanks for your help