main
test-branch
| Line | |
|---|
| 1 | #include <pthread.h>
|
|---|
| 2 |
|
|---|
| 3 | #define SIZE 3
|
|---|
| 4 |
|
|---|
| 5 | pthread_rwlock_t lock;
|
|---|
| 6 | int glob = 0;
|
|---|
| 7 | int y = 0;
|
|---|
| 8 | int * yptr = &y;
|
|---|
| 9 |
|
|---|
| 10 | void * reader(void * arg){
|
|---|
| 11 | pthread_rwlock_rdlock(&lock);
|
|---|
| 12 | *yptr = glob;
|
|---|
| 13 | pthread_rwlock_unlock(&lock);
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | void * writer(void * arg){
|
|---|
| 17 | pthread_rwlock_wrlock(&lock);
|
|---|
| 18 | glob++;
|
|---|
| 19 | pthread_rwlock_unlock(&lock);
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | int main(){
|
|---|
| 24 | pthread_t threads[SIZE];
|
|---|
| 25 | pthread_rwlock_init(&lock, NULL);
|
|---|
| 26 | for(int x = 0; x < SIZE; x++){
|
|---|
| 27 | if(x % 3 == 0){
|
|---|
| 28 | pthread_create(&threads[x], NULL, writer, NULL);
|
|---|
| 29 | }
|
|---|
| 30 | else{
|
|---|
| 31 | pthread_create(&threads[x], NULL, reader, NULL);
|
|---|
| 32 | }
|
|---|
| 33 | }
|
|---|
| 34 | pthread_exit(NULL);
|
|---|
| 35 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.