On Mon, Sep 06, 2004 at 10:38:41PM +0300, David Harel wrote:
> Below pthread code example. 

>   for (i = 0; i < ITERATIONS; i++)
>   {
>       pthread_mutex_lock(&mutex);
>       pthread_mutex_unlock(&mutex);
>       j = myglobal;
>       j++;
>       myglobal = j;
>       printf("%s%d", arg, myglobal);
>   }

Don't do that. Very buggy code. Unlock the mutex *after* you change the global.
On the other hand, using a local variable doesn't buy you anything; just myglobal++ is 
fine, but *when you're holding the mutex*. 

Also, if you want to ensure there's actually any system-level IO in each exectution,
flush with fflush(stdout) after each printf.

> My question.
> What is the scheduling policy? is it not that whenever a thread does IO 
> it looses CPU?

Depends on so many things, like where the output is going (terminal, file, pipe, 
socket), what the file 
system/network stack are, do they have delayed writes, etc. etc. A thread loses CPU 
when it has to wait for a 
physical IO operation to complete. Not every printf() call and not even any write() 
call results in a physical 
IO operation.

Just don't depend on that.

-- 
avva
"There's nothing simply good, nor ill alone" -- John Donne


=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to