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

Asked

Viewed 143 times

1

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

What an equivalent of that would look like? void *reader(void *i). I’ve never seen a structure like this in C++, a pointer of void which has as 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);
}

1 answer

0


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

It’s not a code idiomatic. In C++ would probably use a template in place of a void *, would use a stream in place of printf(), it is possible that other things would be done differently depending on the context, but the code is still C++.

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

  • I solved the problem already, I forgot to mark there as solved, in vdd the code follows the right logic for the solution of the problem... this missing only solve the starvation problem for the algorithm I did. from now on thank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.