Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID


We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

pthread locking and unlocking

MukindMukind Posts: 2Member
When more than one thread is waiting for a locked mutex, which thread will be granted the lock first after it is released.

In our application we want to implement FIFO thread scheduling policy i.e. Thread should get lock which one requested first for it.
AIX have SCHED_FIFO thread scheduling policy but did not worked, i have added code snap shots below with output.

Plz guys help us ...!!

#define NTHREADS 13

pthread_attr_t attr;
pthread_mutex_t mym;
pthread_once_t OnceInit = PTHREAD_ONCE_INIT;
int once = 1;
void *dowork(void *threadid)
pthread_t ThreadID = thread_self();
---------GOING TO LOCK ==[%u]",ThreadID);
if( pthread_mutex_lock (&mym ) != 0)
Error in Locking Mutex");
------- GOT IT MAN----[%u]",ThreadID);
if( once == 1)

---------GOING TO UNLOCK ==[%u]",ThreadID);

if( pthread_mutex_unlock ( &mym ) != 0)
Error in UnLocking Mutex");

void init_routine ()
if( pthread_mutex_init (&mym,NULL ) != 0)
printf("Error in intialising Mutex "),exit(1);

int main(int argc, char *argv[])
pthread_attr_t tattr;
int policy;

pthread_t threads[NTHREADS];
size_t stacksize;
int rc;
long *t;
long i=0;
t = (long *)malloc(sizeof(long));
pthread_once (&OnceInit,init_routine);

BEFORE POLICY-------[%d]",policy);
/*if(pthread_attr_setschedpolicy(&attr,SCHED_FIFO)!= 0)
perror("Error is:"); */
AFTER POLICY-------[%d]",policy);

for(i=0; i<NTHREADS; i++){
rc = pthread_create(&threads[i], &attr, dowork, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d
", rc);

OUTPUT without thread policy

AFTER POLICY-------[0]
---------GOING TO LOCK ==[1111421]
------- GOT IT MAN----[1111421]
[color=Red] ---------GOING TO LOCK ==[465893][/color]
---------GOING TO LOCK ==[167661]
---------GOING TO LOCK ==[1132113]
---------GOING TO LOCK ==[951301]
---------GOING TO LOCK ==[1554371]
---------GOING TO LOCK ==[852815]
---------GOING TO LOCK ==[170119]
---------GOING TO LOCK ==[98149]
---------GOING TO LOCK ==[495807]
---------GOING TO LOCK ==[1330225]
---------GOING TO LOCK ==[438407]
---------GOING TO LOCK ==[731879]
---------GOING TO UNLOCK ==[1111421]
------- GOT IT MAN----[167661]
---------GOING TO UNLOCK ==[167661]
------- GOT IT MAN----[1132113]
---------GOING TO UNLOCK ==[1132113]
------- GOT IT MAN----[951301]
---------GOING TO UNLOCK ==[951301]
------- GOT IT MAN----[1554371]
---------GOING TO UNLOCK ==[1554371]
------- GOT IT MAN----[852815]
---------GOING TO UNLOCK ==[852815]
------- GOT IT MAN----[170119]
---------GOING TO UNLOCK ==[170119]
------- GOT IT MAN----[98149]
---------GOING TO UNLOCK ==[98149]
------- GOT IT MAN----[495807]
---------GOING TO UNLOCK ==[495807]
------- GOT IT MAN----[1330225]
---------GOING TO UNLOCK ==[1330225]
------- GOT IT MAN----[438407]
---------GOING TO UNLOCK ==[438407]
------- GOT IT MAN----[731879]
---------GOING TO UNLOCK ==[731879]
[color=Red] ------- GOT IT MAN----[465893]
---------GOING TO UNLOCK ==[465893]
OUTPUT with thread scheduling policy
After setting FIFO policy

AFTER POLICY-------[1]
---------GOING TO LOCK ==[40339]
------- GOT IT MAN----[40339]
[color=Red] ---------GOING TO LOCK ==[721475][/color]
---------GOING TO LOCK ==[1174377]
---------GOING TO LOCK ==[449527]
---------GOING TO LOCK ==[98151]
---------GOING TO LOCK ==[495809]
---------GOING TO LOCK ==[1330227]
---------GOING TO LOCK ==[438409]
---------GOING TO LOCK ==[731881]
---------GOING TO LOCK ==[161917]
---------GOING TO LOCK ==[1111423]
---------GOING TO LOCK ==[745475]
---------GOING TO LOCK ==[1180995]
---------GOING TO UNLOCK ==[40339]
------- GOT IT MAN----[1174377]
---------GOING TO UNLOCK ==[1174377]
------- GOT IT MAN----[449527]
---------GOING TO UNLOCK ==[449527]
------- GOT IT MAN----[98151]
---------GOING TO UNLOCK ==[98151]
------- GOT IT MAN----[495809]
---------GOING TO UNLOCK ==[495809]
------- GOT IT MAN----[1330227]
---------GOING TO UNLOCK ==[1330227]
------- GOT IT MAN----[438409]
---------GOING TO UNLOCK ==[438409]
------- GOT IT MAN----[731881]
---------GOING TO UNLOCK ==[731881]
------- GOT IT MAN----[161917]
---------GOING TO UNLOCK ==[161917]
------- GOT IT MAN----[1111423]
---------GOING TO UNLOCK ==[1111423]
------- GOT IT MAN----[745475]
---------GOING TO UNLOCK ==[745475]
------- GOT IT MAN----[1180995]
---------GOING TO UNLOCK ==[1180995]
[color=Red] ------- GOT IT MAN----[721475]
---------GOING TO UNLOCK ==[721475]
Sign In or Register to comment.