"void *reader(void *i)" What is the equivalent in C++?



  • I am studying Operating System and have an exercise to do in C++ and I am in doubt (the book only brings in Java), I have to implement a classic reader and writer problem using traffic lights and control variables... And I'm studying some codes. The function below was written in C, but I want to write it in C++.

    How would it look like that? void *reader(void *i). I've never seen a structure like this in C++, a pointer void that has as a parameter a pointer void again or something.

    void *reader(void *i)
    {
        printf("\n-------------------------");
        printf("\n\n reader-%d is reading",i);
    
    sem_wait(&z);
    sem_wait(&rsem);
    sem_wait(&x);
    readcount++;
    if(readcount==1)
    sem_wait(&wsem);
    sem_post(&x);
    sem_post(&rsem);
    sem_post(&z);
    printf("\nupdated value : %d",sh_var);
    sem_wait(&x);
    readcount--;
    if(readcount==0)
    sem_post(&wsem);
    sem_post(&x);
    

    }



  • Virtually every C code is a C++ code, so this is in C++.

    It's not a code https://pt.stackoverflow.com/q/184238/101 . In C++ it would probably use one template in place of a void *, use one https://pt.stackoverflow.com/q/113302/101 in place of printf(), it is possible that other things would be done differently, depending on the context, but the code is no longer C++.

    This code seems to have several errors, there is another problem.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2