Welcome! Log In Create A New Profile

Advanced

LWP_CreateThread fails to create 11th thread [RESOLVED]

Posted by owen 
LWP_CreateThread fails to create 11th thread [RESOLVED]
April 14, 2014 11:00PM
I have been creating alot of threads lately with LWP_CreateThread() but after I create the 11th thread the function stops creating threads. It returns a failure value and I cannot seem to find a way to continue creating threads. I created the threads on demand but there is usually only one or 2 threads are running at any one a time. I always suspend threads after they are done and I reuse the thread pointers. What could the problem be?

this is what I use to create the thread;

	s32 result = LWP_CreateThread(&main_thread[current_thread], thread_func, NULL, 0, STACKSIZE, 66+current_thread);
      if (result == 0) return true;

	if(threading_debug) printf("thread failed to create.\n");
	return false;



Edited 2 time(s). Last edit at 04/17/2014 04:56AM by owen.
Re: LWP_CreateThread fails to create 11th thread
April 15, 2014 02:27AM
You're passing 0 for the stackbase argument so libogc has to allocate the stack for the new thread from its internal pool. That pool only has a limited amount of space so after a few threads it is all used up.
Either use malloc() to allocate STACKSIZE bytes and use that memory for the stackbase argument, or use a static array if you don't want to worry about free()'ing the stack after the threads end.

Edit: Suspending threads does not release their resources, it justs pauses them from running.



Edited 1 time(s). Last edit at 04/15/2014 02:27AM by tueidj.
Re: LWP_CreateThread fails to create 11th thread
April 15, 2014 03:38AM
Oh I see. I have started using a static array for the stack but I still have the problem. How do I release the resources of the old threads so that I can re-create them?



#define THREADMAX 8

#define STACKSIZE 8*1024  

static lwp_t main_thread[THREADMAX];  
lwpq_t thread_queue;

static u8 stack1[THREADMAX][STACKSIZE] ATTRIBUTE_ALIGN (32);

s32 result = LWP_CreateThread(&main_thread[current_thread], thread_func, NULL, stack1[current_thread], STACKSIZE, 63+current_thread);

Re: LWP_CreateThread fails to create 11th thread
April 15, 2014 07:09PM
I think I am suspending my threads instead of letting them run to completion which might be what is causing the problem. I am going to check to see if that is the cause of the problem
Re: LWP_CreateThread fails to create 11th thread
April 16, 2014 11:42PM
I solved the problem. I was mistakenly suspending the threads and not allowing them to come to a natural end. So it works now as intended. I've added in a check to make sure I wait for a free slot to become available so that I don't hit up on the limit. [pastebin.com]
Sorry, only registered users may post in this forum.

Click here to login