Thank you for your elaborate answer.
The code I send was merely for demonstration purposes and that is why you see the global variable.
The mutex is only to make the two threads start almost simultaneously. The main thread locks it and then both threads attempt to lock it. Once the main thread releases it one of the two threads locks and immediately unlock the mutex - by that releases the other thread.
The printf operation is on a null buff using setbuf(stdout, NULL);
I am not sure about it any more, the manual pages do not refer to setbuf with NULL buffer but only to setvbuf. I hope it still means that the output is immediate. About system level cache, Do you think that using fsync will make any difference? cos it didn't
Anatoly Vorobey wrote:
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.
-- Thanks.
David Harel,
==================================
Home office +972 4 6921986
Fax: +972 4 6921986
Cellular: +972 54 4534502
Snail Mail: Amuka
D.N Merom Hagalil
13802
Israel
Email: [EMAIL PROTECTED]================================================================= 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]
