C/C++ on Linux/Unix

Moderators: Lundin
Number of threads: 314
Number of posts: 633

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
pthread locking and unlocking Posted by Mukind on 27 Jul 2012 at 11:42 AM
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 ...!!


#include <pthread.h>
#include <stdio.h>
#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();
printf("\n ---------GOING TO LOCK ==[%u]",ThreadID);
if( pthread_mutex_lock (&mym ) != 0)
{
printf("\n Error in Locking Mutex");
exit(1);
}
printf("\n ------- GOT IT MAN----[%u]",ThreadID);
if( once == 1)
{
++once;
sleep(1);
}
else
sleep(1);

printf("\n ---------GOING TO UNLOCK ==[%u]",ThreadID);

if( pthread_mutex_unlock ( &mym ) != 0)
{
printf("\n Error in UnLocking Mutex");
exit(1);
}
printf("\n ------------BYE---------[%u]",ThreadID);
pthread_exit(NULL);
}

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);

pthread_attr_init(&attr);
pthread_attr_getschedpolicy(&attr,&policy);
printf("\n BEFORE POLICY-------[%d]",policy);
/*if(pthread_attr_setschedpolicy(&attr,SCHED_FIFO)!= 0)
perror("Error is:"); */
pthread_attr_getschedpolicy(&attr,&policy);
printf("\n 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\n", rc);

exit(-1);
}
}
sleep(10);
pthread_exit(NULL);
}
---------------------------------------------------------------------------------------------
OUTPUT without thread policy

BEFORE POLICY-------[0]
AFTER POLICY-------[0]
---------GOING TO LOCK ==[1111421]
------- GOT IT MAN----[1111421]
---------GOING TO LOCK ==[465893]
---------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]
------------BYE---------[1111421]
------- GOT IT MAN----[167661]
---------GOING TO UNLOCK ==[167661]
------------BYE---------[167661]
------- GOT IT MAN----[1132113]
---------GOING TO UNLOCK ==[1132113]
------------BYE---------[1132113]
------- GOT IT MAN----[951301]
---------GOING TO UNLOCK ==[951301]
------------BYE---------[951301]
------- GOT IT MAN----[1554371]
---------GOING TO UNLOCK ==[1554371]
------------BYE---------[1554371]
------- GOT IT MAN----[852815]
---------GOING TO UNLOCK ==[852815]
------------BYE---------[852815]
------- GOT IT MAN----[170119]
---------GOING TO UNLOCK ==[170119]
------------BYE---------[170119]
------- GOT IT MAN----[98149]
---------GOING TO UNLOCK ==[98149]
------------BYE---------[98149]
------- GOT IT MAN----[495807]
---------GOING TO UNLOCK ==[495807]
------------BYE---------[495807]
------- GOT IT MAN----[1330225]
---------GOING TO UNLOCK ==[1330225]
------------BYE---------[1330225]
------- GOT IT MAN----[438407]
---------GOING TO UNLOCK ==[438407]
------------BYE---------[438407]
------- GOT IT MAN----[731879]
---------GOING TO UNLOCK ==[731879]
------------BYE---------[731879]
------- GOT IT MAN----[465893]
---------GOING TO UNLOCK ==[465893]
------------BYE---------[465893]

-----------------------------------------------------------------------------------
OUTPUT with thread scheduling policy
After setting FIFO policy

BEFORE POLICY-------[0]
AFTER POLICY-------[1]
---------GOING TO LOCK ==[40339]
------- GOT IT MAN----[40339]
---------GOING TO LOCK ==[721475]
---------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]
------------BYE---------[40339]
------- GOT IT MAN----[1174377]
---------GOING TO UNLOCK ==[1174377]
------------BYE---------[1174377]
------- GOT IT MAN----[449527]
---------GOING TO UNLOCK ==[449527]
------------BYE---------[449527]
------- GOT IT MAN----[98151]
---------GOING TO UNLOCK ==[98151]
------------BYE---------[98151]
------- GOT IT MAN----[495809]
---------GOING TO UNLOCK ==[495809]
------------BYE---------[495809]
------- GOT IT MAN----[1330227]
---------GOING TO UNLOCK ==[1330227]
------------BYE---------[1330227]
------- GOT IT MAN----[438409]
---------GOING TO UNLOCK ==[438409]
------------BYE---------[438409]
------- GOT IT MAN----[731881]
---------GOING TO UNLOCK ==[731881]
------------BYE---------[731881]
------- GOT IT MAN----[161917]
---------GOING TO UNLOCK ==[161917]
------------BYE---------[161917]
------- GOT IT MAN----[1111423]
---------GOING TO UNLOCK ==[1111423]
------------BYE---------[1111423]
------- GOT IT MAN----[745475]
---------GOING TO UNLOCK ==[745475]
------------BYE---------[745475]
------- GOT IT MAN----[1180995]
---------GOING TO UNLOCK ==[1180995]
------------BYE---------[1180995]
------- GOT IT MAN----[721475]
---------GOING TO UNLOCK ==[721475]
------------BYE---------[721475]




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.