c - How to acquire and release the locks in OpenMP -


this program, creates number of parallel threads , each reader thread cannot read data while writer thread writing it

#include<omp.h> #include<stdio.h> #include<stdlib.h>  omp_lock_t rw_lock; char data[50];  void readdata(int tid){      while(!omp_test_lock(&rw_lock))         printf("\n[thread %d - reader]: waiting data ...",tid);     printf("\n[thread %d - reader]:\t%s",tid,data);     omp_unset_lock(&rw_lock);  }  void writedata(int tid){      while(!omp_test_lock(&rw_lock))         printf("\n[thread %d - writer]: waiting data ...",tid);     printf("\n[thread %d - writer]\n\tenter new data:\t",tid);     scanf(" %[^\n]49s",&data);     omp_unset_lock(&rw_lock); }  int main(){      int read_cnt,write_cnt,wrote=0;      setbuf(stdout,null);      printf("\nenter data:\t");     scanf(" %[^\n]49s",&data);      printf("\nenter total reader , writer thread count:\t");     scanf(" %d%d",&read_cnt,&write_cnt);     omp_init_lock(&rw_lock);      #pragma omp parallel num_threads(read_cnt+write_cnt)     for(int i=0; i<(read_cnt+write_cnt);i++){         int tid = omp_get_thread_num();         if(i<read_cnt){             readdata(tid);         }else             writedata(tid);     }     omp_destroy_lock(&rw_lock);     return 0; } 

command used

gcc -fopenmp readerwriter.c 

this program works 1 writer thread:

enter data: test word  enter total reader , writer thread count: 4 1  [thread 0 - reader]: waiting data ... [thread 1 - reader]:    test word [thread 3 - reader]: waiting data ... [thread 0 - reader]:    test word [thread 2 - reader]: waiting data ... [thread 2 - reader]:    test word [thread 3 - reader]:    test word [thread 4 - writer]     enter new data: hi 

but if input 2 or more writer threads:

[thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ... [thread 0 - reader]: waiting data ...^c 

please help

the order in readers / writers getting lock not defined. in "bad" case, don't see "enter new data:" prompt because reader thread spamming console while trying acquire lock.

the first step rid of output in test_lock loop. replace test-loop single omp_set_lock each.

but still doesn't guarantee order of reading/writing. if want implement real producer/consumer should take care of proper queue. note openmp isn't particularly suited implement producer/consumer programs, won't lots of it.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -