Your replies make a lot of sense. And I appreciate your good-natured acceptance of my criticism of the program. praisetazio wrote:
Alle 21:34, mercoledì 28 gennaio 2004, Steven Augart ha scritto:
Incidentally, document that you use waiting purely for printing debugging messages.
it's used also in an "if" statement.
Oops, you're right. I was already thinking of your program partly as I would have written it, where the pthread_cond_signal() would always happen, not be inside the if() statement.
Now to the meat of the matter, the *pthread* calls:
I have personally always used pthread_cond_wait() from inside of a while() loop, just in case the condition status has changed between the time someone sent the signal and the time the waiting thread received it. This code would be more robust and simpler if you assumed that the condition being signalled did not necessarily mean that there was actually a berth available, just that there might be one. Then, at least while you're debugging, use pthread_cond_broadcast() instead of pthread_cond_signal(). And get rid of the if() statement guarding the pthread_cond_signal (now to be pthread_cond_broadcast()). Just always broadcast; if there are no listeners, the runtime system will throw away the broadcast. You can add the if() back after you have your implementation working in the simple mode.
Yes I figured it out while I was out to eat. A really silly problem after all. Strangely the examples from my teacher do not use always a while statement.
It may be possible to do without the while() statement if you've thought through all the possible interactions. Maybe your teacher is a more thoughtful programmer than I am. It also may be that your teacher is only human and makes mistakes too. In production coding, I use these things as part of writing robust code.
Ma tu sai l'italiano?
Praise
Sì. La mia mamma é abruzzese; é immigrata agli Stati Uniti e dopo si é sposata a un bavarese. Posso comprare Oggi, Gente, La Repubblica, e altre pubblicazioni in edicola (per il meno, nelle tre zone metropolitane in cui ho abitato, quelle di Los Angeles, Boston, e Nuova York.) Mi abbono a "America Oggi" e all'edizione italiana di "Oggi." --Stefano (Steve) -- Steven Augart Jikes RVM, an open source Java Virtual Machine http://oss.software.ibm.com/jikesrvm Office: +1 914/784-6743 T.J. Watson Research Center, IBM