Title: RE: pthrread question.

1) The global variable operation is atomic so you can do without a mutex (at least with one CPU).
   You should add a volatile prefix (to the declaration or at least during access).
2) It is hard to know what the IO does. Either fill a static array to see real behaviors without any
   IO pending or, write directly to disk to make sure you have a pending situation.
3) A basic design assumption for multithreading program is that you can't know where the
   preemption, yielding or context switch occur. You only do a rendezvous or synchronization
   when it is necessary.



>> -----Original Message-----
>> From: [EMAIL PROTECTED]
>> [mailto:[EMAIL PROTECTED]] On Behalf Of David Harel
>> Sent: Monday, September 06, 2004 9:39 PM
>> To: linux-il
>> Subject: pthrread question.
>>
>>
>> Hi,
>>
>> Below pthread code example. When running this code I expect
>> each thread
>> run until it does IO (printf). At this point I expect the
>> thread to be
>> blocked and the other thread to run until it does IO therefore the
>> threads should share the CPU evenly one at a time. However
>> if you look
>> down the printout you will see that first 40 times are
>> executed by one
>> thread and then another 40 times by the other threads.
>> When I increase the ITERATIONS from 40 to 400 I see that around the
>> value 280 the threads exchange.
>>
>> My question.
>> What is the scheduling policy? is it not that whenever a
>> thread does IO
>> it looses CPU?
>>
>> Execution printout.
>> -1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-
>> 24-25-26-27-28-29-30-31-32-33-34-35-36-37-38-39-40+41+42+43+4
>> 4+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64
>> +65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80
>>
>> Here is the code:
>> #include <pthread.h>
>> #include <stdio.h>
>> #include <errno.h>
>>
>> #define ITERATIONS 40
>>
>> int myglobal;
>> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
>>
>> void *thread_function(void *arg)
>> {
>>    int i, j;
>>
>>    for (i = 0; i < ITERATIONS; i++)
>>    {
>>        pthread_mutex_lock(&mutex);
>>        pthread_mutex_unlock(&mutex);
>>        j = myglobal;
>>        j++;
>>        myglobal = j;
>>        printf("%s%d", arg, myglobal);
>>    }
>> }
>>
>> int main()
>> {
>>     pthread_t mythread1, mythread2;
>>     pthread_attr_t trattr;
>>   
>>     pthread_attr_init(& trattr);
>>     pthread_attr_setscope(& trattr, PTHREAD_SCOPE_SYSTEM);
>>
>>     setbuf(stdout, NULL);
>>
>>     pthread_mutex_lock(&mutex);
>>     if (pthread_create(&mythread1, & trattr, thread_function, "-"))
>>     {
>>         perror("pthread_create");
>>         exit(errno);
>>     }
>>
>>     if (pthread_create(&mythread2, & trattr, thread_function, "+"))
>>     {
>>         perror("pthread_create");
>>         exit(errno);
>>     }
>>     sleep(1); // we have the two threads up by now.
>>     pthread_mutex_unlock(&mutex);//Unlock the mutex will releas them
>> almost symultanously.
>>     if (pthread_join(mythread1, NULL))
>>     {
>>         perror("pthread_join");
>>         exit(errno);
>>     }
>>     if (pthread_join(mythread2, NULL))
>>     {
>>         perror("pthread_join");
>>         exit(errno);
>>     }
>>
>>     printf("\nmyglobal = %d\n", myglobal);
>> }
>>
>> --
>> 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]
>>

 
 
This e-mail message has been sent by Elbit Systems Ltd.
and is for the use of the intended recipients only.
The message may contain privileged or commercial confidential information .
If you are not the intended recipient you are hereby notified that any use,
distribution or copying of this communication is strictly prohibited,
and you are requested to delete the e-mail and any attachments
and notify the sender immediately.

Reply via email to