Scheduler Issue on L4T R21.3 on Jetson TK1

Dear All,

I am trying to run simple multi-threaded application on Jetson TK1. Code snippet is as follows:

void* DoWork(void* args)
{
while(1){
int nr = (int)args;
printf(“Wątek: %d, ID: %d, CPU: %d\n”, nr,pthread_self(), sched_getcpu());
}
}

void* DoWork_Again(void* args)
{
while(1){
int nr = (int)args;
printf(“DoWok_AGain: %d, ID: %d, CPU: %d\n”, nr,pthread_self(), sched_getcpu());
}
}

int main()
{
int count = 2;
pthread_t threads[count];
pthread_create(&threads[0], NULL, DoWork, (void*)1);
pthread_create(&threads[1], NULL, DoWork_Again, (void*)2);
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
}

Both thread function contains while(1)…

Problem i observed here is second thread(DoWork_Again) is not getting spawn.but if i add sleep(1) in both thread function then both thread function run…

Why is this things are happening ? What is usefulness of threads if we have to add sleep to get it scheduled ?

Any insights are highly appreciated…

First, there’s an out of bounds array call ( threads[2] ) , this is probably just a transcription error from your actual program.

Second, from http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_join.html

The function pthread_join works like this :

The pthread_join() function shall suspend execution of the calling thread until the target thread terminates, unless the target thread has already terminated.

Money quote:
The pthread_join() function provides a simple mechanism allowing an application to wait for a thread to terminate.

So in the pseudo example code, two new threads were created, the first thread is joined while the calling thread is suspended. And so it lives happily ever after, wandering around exploring the while loop on the first created thread.

First, there’s an out of bounds array call ( threads[2] ) , this is probably just a transcription error from your actual program.

------ This was by mistake… i corrected it…

So in the pseudo example code, two new threads were created, the first thread is joined while the calling thread is suspended. And so it lives happily ever after, wandering around exploring the while loop on the first created thread.

------ My query was here second thread is not getting spawn. i can not see " printf(“DoWok_AGain: %d, ID: %d, CPU: %d\n”, nr,pthread_self(), sched_getcpu()); statement executing on terminal…
I believe pthread_join’s role here is in main function to wait for two threads to complete and then only return from main function…

Please correct me if i am wrong…

Hello,
Can you run the correct program for long time and check the log carefully? both thread can run. Maybe the log is flooded and you missed it. No need for sleep.

br
ChenJian