Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pthread RWlock查询_C_Pthreads - Fatal编程技术网

pthread RWlock查询

pthread RWlock查询,c,pthreads,C,Pthreads,我用翻译是因为我英语不好 #include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <fcntl.h> #include <unistd.h> pthread_rwlock_t R_Lock; int ac=0; void* RD_LOCK(void* arg) {

我用翻译是因为我英语不好

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>

pthread_rwlock_t R_Lock;
int ac=0;  

void* RD_LOCK(void* arg) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    pthread_rwlock_rdlock(&R_Lock);                                                                                                                                                                                                              
    printf("%d\n",ac); // ac is global variable 0                                                                                                                                                                                                
    sleep(1);                                                                                                                                                                                                                                    
    pthread_rwlock_unlock(&R_Lock); 
}                                                                                                                                                                                                                                                                                                                                                                                                                                                       
void* WR_LOCK(void* arg) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    pthread_rwlock_wrlock(&R_Lock);                                                                                                                                                                                                              
    printf("RW LCOK\n");                                                                                                                                                                                                                         
    ac++;                                                                                                                                                                                                                                        
    printf("%d\n",ac);                                                                                                                                                                                                                           
    sleep(1);                                                                                                                                                                                                                                    
    pthread_rwlock_unlock(&R_Lock);                                                                                                                                                                                                            
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
void DO_RW_LCOK(){                                                                                                                                                                                                                                
    pthread_t RW[3];                                                                                                                                                                                                                             
    pthread_rwlock_init(&R_Lock, NULL);                                                                                                                                                                                                                                                                                                                                                                                                                                                       
    pthread_create(&RW[0], NULL, RD_LOCK,NULL); //==0 thread                                                                                                                                                                                     
    pthread_create(&RW[1], NULL, RD_LOCK,NULL); //==1 thread                                                                                                                                                                                     
    pthread_create(&RW[2], NULL, WR_LOCK,NULL); //==2 thread                                                                                                                                                                                                                                                                                                                                                                                                                                  
    pthread_rwlock_destroy(&R_Lock);                                                                                                                                                                                                                                                                                                                                                                                                                                                          
    for(int i=0; i<3;i++)                                                                                                                                                                                                                        
    pthread_join(RW[i],NULL);                                                                                                                                                                                                               
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
int main(){                                                                                                                                                                                                                                    
    DO_RW_LCOK();                                                                                                                                                                                                                                
    return 0;                                                                                                                                                                                                                                  
}  

void* RD_LOCK(void* arg) {

  pthread_rwlock_rdlock(&R_Lock);
  printf("%d\n",ac); // ac is global variable 0
  sleep(1);
  pthread_rwlock_unlock(&R_Lock); }

void* WR_LOCK(void* arg) {

  pthread_rwlock_wrlock(&R_Lock);
  ac++;
  printf("%d\n",ac);
  sleep(1);
  pthread_rwlock_unlock(&R_Lock);
}
int main(){
  DO_RW_LOCK();
  return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
pthread_rwlock_t R_Lock;
int ac=0;
void*RD_锁(void*arg){                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
pthread_rwlock_rdlock(&R_Lock);
printf(“%d\n”,ac);//ac是全局变量0
睡眠(1);
pthread_rwlock_unlock(&R_Lock);
}                                                                                                                                                                                                                                                                                                                                                                                                                                                       
void*WR_锁(void*arg){                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
pthread_rwlock_wrlock(&R_Lock);
printf(“RW LCOK\n”);
ac++;
printf(“%d\n”,ac);
睡眠(1);
pthread_rwlock_unlock(&R_Lock);
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
void DO_RW_LCOK(){
pthread_t RW[3];
pthread\u rwlock\u init(&R\u Lock,NULL);                                                                                                                                                                                                                                                                                                                                                                                                                                                       
pthread_create(&RW[0],NULL,RD_LOCK,NULL);//==0线程
pthread_create(&RW[1],NULL,RD_LOCK,NULL);//==1个线程
pthread_create(&RW[2],NULL,WR_LOCK,
Lock waitAndNotify;
int rdDone = 0;
void* RD_LOCK(void* arg) {
  pthread_rwlock_rdlock(&R_Lock);
  rdDone++;
  notify(waitAndNotify);
  printf("%d\n",ac); // ac is global variable 0
  sleep(1);
  pthread_rwlock_unlock(&R_Lock); }

void* WR_LOCK(void* arg) {

  pthread_rwlock_wrlock(&R_Lock);
  if(rdDone!=2)
    wait(waitAndNotify);
  ac++;
  printf("%d\n",ac);
  sleep(1);
  pthread_rwlock_unlock(&R_Lock);
}