Created
December 16, 2015 16:10
-
-
Save bachue/b81ab3875861122dd546 to your computer and use it in GitHub Desktop.
Test POSIX thread rwlock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
#include <errno.h> | |
#include <string.h> | |
#define ERROR_PRINTF(fmt, args...) { \ | |
fprintf(stderr, fmt, args); \ | |
_exit(EXIT_FAILURE); \ | |
} | |
pthread_rwlock_t rwlock; | |
void *rw_rdlock_test(void *_payload) { | |
int i; | |
long test_id = (long) _payload; | |
for (i = 0; i < 10; ++i) { | |
if (pthread_rwlock_rdlock(&rwlock)) | |
ERROR_PRINTF("Failed to lock %ld for read: %s\n", test_id, strerror(errno)); | |
printf("Lock %ld for read is going to sleep ...\n", test_id); | |
sleep(5); | |
printf("Lock %ld for read is awake\n", test_id); | |
if (pthread_rwlock_unlock(&rwlock)) | |
ERROR_PRINTF("Failed to unlock %ld for read: %s\n", test_id, strerror(errno)); | |
} | |
return NULL; | |
} | |
void *rw_wrlock_test(void *_payload) { | |
int i; | |
long test_id = (long) _payload; | |
for (i = 0; i < 10; ++i) { | |
if (pthread_rwlock_wrlock(&rwlock)) | |
ERROR_PRINTF("Failed to lock %ld for write: %s\n", test_id, strerror(errno)); | |
printf("Lock %ld for write is going to sleep ...\n", test_id); | |
sleep(5); | |
printf("Lock %ld for write is awake\n", test_id); | |
if (pthread_rwlock_unlock(&rwlock)) | |
ERROR_PRINTF("Failed to unlock %ld for write: %s\n", test_id, strerror(errno)); | |
} | |
return NULL; | |
} | |
int main(int argc, char const *argv[]) { | |
long i; | |
pthread_t threads[10]; | |
pthread_rwlockattr_t attr; | |
if (pthread_rwlockattr_init(&attr)) | |
ERROR_PRINTF("Failed to initialize rwlockattr: %s\n", strerror(errno)); | |
if (pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP)) | |
ERROR_PRINTF("Failed to setkind np for rwlockattr: %s\n", strerror(errno)); | |
if (pthread_rwlock_init(&rwlock, &attr)) | |
ERROR_PRINTF("Failed to initialize rwlock: %s\n", strerror(errno)); | |
if (pthread_rwlockattr_destroy(&attr)) | |
ERROR_PRINTF("Failed to destroy rwlockattr: %s\n", strerror(errno)); | |
for (i = 0; i < 5; ++i) { | |
if (pthread_create(&threads[i], NULL, rw_rdlock_test, (void *) i)) | |
ERROR_PRINTF("Failed to create thread %ld: %s\n", i, strerror(errno)); | |
} | |
for (; i < 10; ++i) { | |
if (pthread_create(&threads[i], NULL, rw_wrlock_test, (void *) i)) | |
ERROR_PRINTF("Failed to create thread %ld: %s\n", i, strerror(errno)); | |
} | |
for (i = 0; i < 10; ++i) { | |
if (pthread_join(threads[i], NULL)) | |
ERROR_PRINTF("Failed to join thread %ld: %s\n", i, strerror(errno)); | |
} | |
if (pthread_rwlock_destroy(&rwlock)) | |
ERROR_PRINTF("Failed to destroy rwlock: %s\n", strerror(errno)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment