indent
This commit is contained in:
parent
5eb27bc4fd
commit
1d845ec6a6
@ -8,87 +8,87 @@ void checkResults(const char *s, int rc)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_rwlock_t rwlock;
|
pthread_rwlock_t rwlock;
|
||||||
|
|
||||||
void *rdlockThread(void *arg)
|
void *rdlockThread(void *arg)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
printf("Entered thread, getting read lock\n");
|
printf("Entered thread, getting read lock\n");
|
||||||
rc = pthread_rwlock_rdlock(&rwlock);
|
rc = pthread_rwlock_rdlock(&rwlock);
|
||||||
checkResults("pthread_rwlock_rdlock()\n", rc);
|
checkResults("pthread_rwlock_rdlock()\n", rc);
|
||||||
printf("got the rwlock read lock\n");
|
printf("got the rwlock read lock\n");
|
||||||
|
|
||||||
sleep(5);
|
sleep(5);
|
||||||
|
|
||||||
printf("unlock the read lock\n");
|
printf("unlock the read lock\n");
|
||||||
rc = pthread_rwlock_unlock(&rwlock);
|
rc = pthread_rwlock_unlock(&rwlock);
|
||||||
checkResults("pthread_rwlock_unlock()\n", rc);
|
checkResults("pthread_rwlock_unlock()\n", rc);
|
||||||
printf("Secondary thread unlocked\n");
|
printf("Secondary thread unlocked\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *wrlockThread(void *arg)
|
void *wrlockThread(void *arg)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
printf("Entered thread, getting write lock\n");
|
printf("Entered thread, getting write lock\n");
|
||||||
rc = pthread_rwlock_wrlock(&rwlock);
|
rc = pthread_rwlock_wrlock(&rwlock);
|
||||||
checkResults("pthread_rwlock_wrlock()\n", rc);
|
checkResults("pthread_rwlock_wrlock()\n", rc);
|
||||||
|
|
||||||
printf("Got the rwlock write lock, now unlock\n");
|
printf("Got the rwlock write lock, now unlock\n");
|
||||||
rc = pthread_rwlock_unlock(&rwlock);
|
rc = pthread_rwlock_unlock(&rwlock);
|
||||||
checkResults("pthread_rwlock_unlock()\n", rc);
|
checkResults("pthread_rwlock_unlock()\n", rc);
|
||||||
printf("Secondary thread unlocked\n");
|
printf("Secondary thread unlocked\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int rc=0;
|
int rc = 0;
|
||||||
pthread_t thread, thread1;
|
pthread_t thread, thread1;
|
||||||
|
|
||||||
printf("Enter Testcase - %s\n", argv[0]);
|
printf("Enter Testcase - %s\n", argv[0]);
|
||||||
|
|
||||||
printf("Main, initialize the read write lock\n");
|
printf("Main, initialize the read write lock\n");
|
||||||
rc = pthread_rwlock_init(&rwlock, NULL);
|
rc = pthread_rwlock_init(&rwlock, NULL);
|
||||||
checkResults("pthread_rwlock_init()\n", rc);
|
checkResults("pthread_rwlock_init()\n", rc);
|
||||||
|
|
||||||
printf("Main, grab a read lock\n");
|
printf("Main, grab a read lock\n");
|
||||||
rc = pthread_rwlock_rdlock(&rwlock);
|
rc = pthread_rwlock_rdlock(&rwlock);
|
||||||
checkResults("pthread_rwlock_rdlock()\n",rc);
|
checkResults("pthread_rwlock_rdlock()\n", rc);
|
||||||
|
|
||||||
printf("Main, grab the same read lock again\n");
|
printf("Main, grab the same read lock again\n");
|
||||||
rc = pthread_rwlock_rdlock(&rwlock);
|
rc = pthread_rwlock_rdlock(&rwlock);
|
||||||
checkResults("pthread_rwlock_rdlock() second\n", rc);
|
checkResults("pthread_rwlock_rdlock() second\n", rc);
|
||||||
|
|
||||||
printf("Main, create the read lock thread\n");
|
printf("Main, create the read lock thread\n");
|
||||||
rc = pthread_create(&thread, NULL, rdlockThread, NULL);
|
rc = pthread_create(&thread, NULL, rdlockThread, NULL);
|
||||||
checkResults("pthread_create\n", rc);
|
checkResults("pthread_create\n", rc);
|
||||||
|
|
||||||
printf("Main - unlock the first read lock\n");
|
printf("Main - unlock the first read lock\n");
|
||||||
rc = pthread_rwlock_unlock(&rwlock);
|
rc = pthread_rwlock_unlock(&rwlock);
|
||||||
checkResults("pthread_rwlock_unlock()\n", rc);
|
checkResults("pthread_rwlock_unlock()\n", rc);
|
||||||
|
|
||||||
printf("Main, create the write lock thread\n");
|
printf("Main, create the write lock thread\n");
|
||||||
rc = pthread_create(&thread1, NULL, wrlockThread, NULL);
|
rc = pthread_create(&thread1, NULL, wrlockThread, NULL);
|
||||||
checkResults("pthread_create\n", rc);
|
checkResults("pthread_create\n", rc);
|
||||||
|
|
||||||
sleep(5);
|
sleep(5);
|
||||||
printf("Main - unlock the second read lock\n");
|
printf("Main - unlock the second read lock\n");
|
||||||
rc = pthread_rwlock_unlock(&rwlock);
|
rc = pthread_rwlock_unlock(&rwlock);
|
||||||
checkResults("pthread_rwlock_unlock()\n", rc);
|
checkResults("pthread_rwlock_unlock()\n", rc);
|
||||||
|
|
||||||
printf("Main, wait for the threads\n");
|
printf("Main, wait for the threads\n");
|
||||||
rc = pthread_join(thread, NULL);
|
rc = pthread_join(thread, NULL);
|
||||||
checkResults("pthread_join\n", rc);
|
checkResults("pthread_join\n", rc);
|
||||||
|
|
||||||
rc = pthread_join(thread1, NULL);
|
rc = pthread_join(thread1, NULL);
|
||||||
checkResults("pthread_join\n", rc);
|
checkResults("pthread_join\n", rc);
|
||||||
|
|
||||||
rc = pthread_rwlock_destroy(&rwlock);
|
rc = pthread_rwlock_destroy(&rwlock);
|
||||||
checkResults("pthread_rwlock_destroy()\n", rc);
|
checkResults("pthread_rwlock_destroy()\n", rc);
|
||||||
|
|
||||||
printf("Main completed\n");
|
printf("Main completed\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user