Rockbox Development > Starting Development and Compiling
Threading, how many threads simultaneously?
(1/1)
pablito:
Hi,
I'm developing some drum plugin for rockbox, it is nearly ready for release, but we heave encountered difficulties regarding threading on rockbox when the plugin is run on the machine (simulator is OK).
I have wrote some tests to check some utilities in the plugin, so actually the real plugin will not be so resource consuming. But anyway my questions are:
- how many threads can be created in rockbox?
- is it possible to run threads exit repeatedly?
I have the following code, which stops in the assertion at the end (implemented as splash message). It seems that only 3 threads were actually running. Am I doing something wrong? The actual value of the variable 'counter' in the end is 30000. This was running on iRiver H340.
// code excerpt
static int counter = 0;
static struct mutex mtx;
static long stack1[DEFAULT_STACK_SIZE];
static long stack2[DEFAULT_STACK_SIZE];
static long stack3[DEFAULT_STACK_SIZE];
static long stack4[DEFAULT_STACK_SIZE];
static long stack5[DEFAULT_STACK_SIZE];
static
void do_something( void )
{
unsigned max;
for ( max = 0; max < 10000; max++ ) {
rb->mutex_lock(&mtx);
counter ++;
rb->mutex_unlock(&mtx);
}
rb->thread_exit();
}
void test_mutex( void )
{
rb->mutex_init(&mtx);
unsigned thread1;
unsigned thread2;
unsigned thread3;
unsigned thread4;
unsigned thread5;
thread1 = rb->create_thread(do_something, stack1, sizeof(stack1), 0, "test1" IF_PRIO(, PRIORITY_BACKGROUND) IF_COP(, CPU));
thread2 = rb->create_thread(do_something, stack2, sizeof(stack2), 0, "test2" IF_PRIO(, PRIORITY_BACKGROUND) IF_COP(, CPU));
thread3 = rb->create_thread(do_something, stack3, sizeof(stack3), 0, "test3" IF_PRIO(, PRIORITY_BACKGROUND) IF_COP(, CPU));
thread4 = rb->create_thread(do_something, stack4, sizeof(stack4), 0, "test4" IF_PRIO(, PRIORITY_BACKGROUND) IF_COP(, CPU));
thread5 = rb->create_thread(do_something, stack5, sizeof(stack5), 0, "test5" IF_PRIO(, PRIORITY_BACKGROUND) IF_COP(, CPU));
rb->thread_wait(thread1);
rb->thread_wait(thread2);
rb->thread_wait(thread3);
rb->thread_wait(thread4);
rb->thread_wait(thread5);
/* this is to test that the mutex has protected the variable */
assert( counter == 50000 );
}
torne:
You can't create that many threads, no. The maximum number of threads on a swcodec target with recording (like your h340) is 17, but most of those are already used by the system threads. There are only 2-3 spare slots (the plugin's main thread doesn't take a slot because plugin code by default runs in the already-existing UI thread), so you can't create more than 2-3 more threads.
create_thread will return NULL when there is no room to create another thread, so you should be checking that.
pablito:
OK, thanks for figuring out.
p.f.
Navigation
[0] Message Index
Go to full version