Received: from PACIFIC-CARRIER-ANNEX.MIT.EDU by po7.MIT.EDU (5.61/4.7) id AA10333; Mon, 26 Feb 96 09:43:09 EST
Received: from caliban.physics.utoronto.ca by MIT.EDU with SMTP
	id AA23855; Mon, 26 Feb 96 09:42:34 EST
Received: (from neufeld@localhost) by caliban.physics.utoronto.ca (8.6.10/8.6.9) id JAA06756 for proven@MIT.EDU; Mon, 26 Feb 1996 09:43:20 -0500
From: Christopher Neufeld <neufeld@physics.utoronto.ca>
Message-Id: <199602261443.JAA06756@caliban.physics.utoronto.ca>
Date: Mon, 26 Feb 1996 09:43:20 EST
In-Reply-To: Christopher Provenzano <proven@MIT.EDU>
       "Re: pthreads bug?" (Feb 22,  3:17pm)
X-Mailer: Mail User's Shell (7.2.5 10/14/92)
To: Christopher Provenzano <proven@MIT.EDU>
Subject: Re: pthreads bug?

   Christopher,

On Feb 22,  3:17pm, Christopher Provenzano wrote:
>
>>    I'm just learning POSIX threaded programming, and am using the pthreads
>> 1.60 beta 2 included in the Linux libc v5.2.18. To start out, I've been
>> doing simple stuff, including one program which spawns three threads, each
>> of which loops 100000 times, locking a global mutex, unlocking it, then
>> doing some arithmetic. This gives me three processes fighting over the
>> mutex some half a million times a second (this is a Pentium 90).
>>    The program behaviour is sporadic. Sometimes it runs through without
>> trouble, sometimes it locks up, sometimes it runs at much reduced speed.
>> Looking at the code for pthreads, my quick guess is that there might be a
>> problem because pthread_sched_prevent, which appears to be a lock
>> variable to prevent a thread switch during pthread_mutex_lock, is not
>> atomic with the pthread_mutex_lock call. If one thread enters
>> pthread_mutex_lock, and another enters before the pthread_sched_prevent
>> call completes, then we have two threads executing pthread_mutex_lock on
>> the same mutex.
>
>Please send the code. I'll give it a whirl. I doubt it's what you describe
>but there may be a problem.
>
>The routine pthread_sched_prevent() should do an atomic set of 
>pthread_kernel_lock. (Looking at the code I found that it was an int
>and it should be a sig_atomic_t but under linux I think they are the
>same.) So long as this is set no preemption is possible and since this 
>is a user library only one thread is really running at a time. (Kernel 
>thread support is in the works and will be available in later releases.)
>
   OK, I think I was misunderstanding the behaviour. I thought that the
thread creation made a new entry in the kernel's process list, and then
normal multiprocessing procedures were applied at the kernel level. I
gather from your description that a user-defined timed interrupt is used
to trigger the ``context switch'', and that the context switch is done by
user code instead of by the existing kernel support. I had figured that
there was some black magic which prevented a process from entering a
pthread_* function while the pthread_kernel_lock was high. In that case,
a context switch between the completion of the stack push for the
function's arguments and the call to pthread_sched_prevent() would give
the non-atomic behaviour I was thinking of. Clearly your own signal
handler can check the flag, and can be assured that no other thread has
touched it, since it's the signal handler's job to do the context switch,
and so your change is atomic.
   The "lockstep" behaviour you describe is going on. The program
appeared to freeze solid but since I was only providing a "still-alive"
message every 1000 iterations, it was really the result of the slowing
down. It seems to lockstep with two different speeds. One slow, and the
other quite a bit faster, but still slower than the flat-out run. This is
with three threads going simultaneously working on the same mutex. That's
what made me think that sometimes it slows down, and sometimes it freezes
altogether.

>Fixing this is one of the harder problems with threads. Basicly you need to
>design you program to not have these situations just as you would design
>a program to not call read() for every byte in a file.
>
   Yes, my actual application won't be doing this, it will be treating
things more sanely. I had just written my first threading program,
intended to check the reentrancy of random(). The libc is supposed to be
thread-safe, but I had to be sure. The random() call is one which is used
in my code (it's a Monte Carlo simulation), has a global state variable
which is modified during the execution, and produces a repeatable
sequence of numbers which are not evenly spaced. The program I wrote
first called random() 300000 times, adding the results up. Then, it
reseeded the random number generator and spawned three threads, each of
which called random() 100000 times, adding them up and then returning
them to the calling process. About three quarters of the time the numbers
came up the same, the rest of the time they didn't. I sent a copy to
H.J., the fellow who maintains the libc, but haven't heard back from him.
I'll clip it at the bottom of this message in case you're interested.

   Anyway, thank you for your help. Do you have any idea when kernel
support for threading will be available? I just convinced the people here
to buy a second CPU for our SMP box, so that I could parallelize the
program and reduce the runtime, but if it's running in user mode with
timed interrupts making fake context switches, then the second CPU will
just sit there eating up money.

   Congratulations on a nice threading package, and thanks again for the
help.

begin 644 randoms.tar.Z
M'YV04HH$(=*D"("#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MNH6#"N'CPW9&OGVD\[=AEMUCLL54%C7/!!R=,Q!-.%!%"
M>FE_-VT)9G0T.615C4Y&PP\#%SH;F)V.K!R*@^`W&8!O^T5AA*]A..)\,RTL
M9+R%8=?5;SSM==\@XEY6>3%@5IX,TM,Q`\YRGS%OW<_2@=2T5UH$0Q5,F#W'
MQRC$P#?S1^*.^!=VI4&'X>(&C@=F3I#/Q/H$&PP9PB*H`F3:!Q<=L*YVC`,-
MVP;DMG>Y(5[:HYNS1#8#\5F$?+7#W/\2EL#1F8%UF$G@^EITOOF`3'UFTR`=
M`$@%`I+!@`[C'P@2&+`4-B]W@HO?_`YG/_SI3X:R2]:TKG<:@MUP#.=!%LE`
M8`+H^9`)3,`,\K+VMKV]+6I3*T/5V)`"*6(M+7@SP1B\E\$C)O%G3)S>$Z-(
MLB^2#&\IN*+4J/:4+K8Q>6!,@1C)>#DSXD:)*$BC]=;H13R^T8HJP"(=N5C(
MK(5QC+0S&\]\1K(@(B4$TX*!R/;0!QNV#X?<(DU1Y/"&//S.?F7\Y/M$Z;U2
MT@]X*?3?"CGXG<]X$(0SK&4>9%BTH\U%8`I@VJ"&2<QB&O.8R$RF,I?)S&8Z
M\YG0C*8TITG-:EKSFMC,IC:WR<UN>O.;X`RG.,=)SG*:\YSH3*<ZU\G.=KKS
MG?",ISSG2<]ZVO.>^,RG/O?)SW[Z\Y\`#:A`!TK0@AKTH`A-J$(7RM"&.O2A
-$(VH1"=*T8I:]*+-!,RG
`
end


-- 
 Christopher Neufeld                   neufeld@physics.utoronto.ca
 Home page:  http://caliban.physics.utoronto.ca/neufeld/Intro.html
 "Don't edit reality for the sake of simplicity"
