HemoGoblin
HemoGoblin

Reputation: 27

C Language Increment(I Think)

I'll start by saying it's a homework thing, but its got nothing to do with me learning C. We are tasked to implement solutions to the Reader/Writer conflict using Semaphores and priority. Doing so in Java for the class.

The book we are working with, however uses C(I think) to handle their code examples. I'm not all that familiar with standard C and I can't figure out how to search existing literature for the answer I seek.

In the code:

semaphore x=1,wsem=1;
int readcount;

void reader()
{
    While (true){
    semWait(x);
    readCount++;
    if(readcount==1)
    {
        semWait(wsem);
    }
    semSignal(x);
    READUNIT();
    semWait(x);
    readcount; /* <--- the questionable command */
    if(readcount==0)
    {
        semSignal(wsem)
    }
    semSignal(x);
    }
}

The line I have "starred" doesn't make any apparent sense. I appears to be simply stating or declaring the name of the variable. Is this some form of decrement I've never seen, or does this do something else? It seems like I'd be able to find it in some C-guide somewhere, but I haven't a clue what it's doing so I don't really know how to ASK what it's doing.

Upvotes: 1

Views: 162

Answers (1)

Russell Borogove
Russell Borogove

Reputation: 19037

A variable name followed by a semicolon is a complete legal statement in C, but it does nothing. The form is rarely used, although it can be useful to suppress unused-variable warnings in some compilers.

From context, what it was probably supposed to be is readcount--;. That's the C decrement operator, undoing the readcount++ above.

Does your source also have the (erroneous) capital-W While and inconsistent spellings of readcount? If so, that's several bad typos in one short example and you should be suspicious of everything else in the book.

Upvotes: 1

Related Questions