Assiging thread priority

Hi,
I am a beginner in QNX and need some help.

I am trying to assign two threads (ThreadA & ThreadB), in a same process, two different priorities. The scheduling policy is FIFO. I was hoping to see the high priority thread pre-empt the execution of the low priority thread. But that is not happening.

Can anyone help me on this? The code is attached with this message. THANKS.

/----------------------------CODE ------------------------------------------/
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
//--------------------------------------------------------------------
void *
funcThreadA( void )
{
struct sched_param params;
int ret, prio;
int a_loop;

for ( a_loop = 1; a_loop <= 10; a_loop++ )
{
	ret = pthread_getschedparam(pthread_self(), &prio, &params);
	if(ret != 0)
		printf("pthread_getschedparam() failed %d \n", errno);
	else 
		printf("pri %d (cur %d) ",params.sched_priority, params.sched_curpriority);
	
	printf( "A\n" );
	sleep( 1 );
}
return;

}

//--------------------------------------------------------------------
void *
funcThreadB( void )
{
struct sched_param params;
int ret, prio;
int b_loop;

for ( b_loop = 1; b_loop <= 5; b_loop++ )
{
	ret = pthread_getschedparam(pthread_self(), &prio, &params);
	if(ret != 0)
		printf("pthread_getschedparam() failed %d \n", errno);
	else 
		printf("pri %d (cur %d) ",params.sched_priority, params.sched_curpriority);

	printf( "B\n" );
	sleep( 2 );
}
return;

}

//--------------------------------------------------------------------
int
main( void )
{
struct sched_param params;
pthread_attr_t attr;
int ret;

pthread_t threadA;
pthread_t threadB;

printf("# Set attributes ...\n");
pthread_attr_init(&attr);
ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
if(ret != 0) {
	printf("pthread_attr_setinheritsched() failed %d \n", errno);
	return 1;
}

ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
if(ret != 0) {
	printf("pthread_attr_setschedpolicy() failed %d %d\n", ret, errno);
	return 1;
} 
//---------------------------------------------------------//
params.sched_priority = 12;

ret = pthread_attr_setschedparam(&attr, &params);
if(ret != 0) {
	printf("pthread_attr_setschedparam(A) failed %d \n", errno);
	return 1;
}

printf("# Creating thread A... ");
ret = pthread_create(&threadA, &attr, &funcThreadA, NULL);
if(ret != 0) {
	printf("pthread_create(A) failed %d \n", errno);
	return 1;
}
//-----------------------------------------------------//
	params.sched_priority = 13;

ret = pthread_attr_setschedparam(&attr, &params);
if(ret != 0) {
	printf("pthread_attr_setschedparam(B) failed %d \n", errno);
	return 1;
}

printf("# Creating thread B... ");
ret = pthread_create(&threadB, &attr, &funcThreadB, NULL);
if(ret != 0) {
	printf("pthread_create(B) failed %d \n", errno);
	return 1;
}
//---------------------------------------------------------//

printf( "Main program waiting\n" );
pthread_join( threadA, NULL );
pthread_join( threadB, NULL );
printf( "Main program ending\n" );
return 0;

}

Hi,
I have just found the dumb mistake … I was using the sleep command in the thread functions and ofcourse when a high priority thread goes to sleep the low priority READY thread will start.

SORRY …:frowning: