hrm...

Bryce McKinlay bryce@waitaki.otago.ac.nz
Sat Jul 20 01:13:00 GMT 2002


Adam Megacz wrote:
>In TinySSL, I generate some entropy like this:
>> static class entropySpinner extends Thread {
> volatile boolean stop = false;
> byte counter = 0;
> entropySpinner() { start(); }
> public void run() {
> System.out.println("start!");
> while (!stop) counter++;
> System.out.println("stop!");
> }
> }
>> entropySpinner[] spinners = new entropySpinner[10];
> for(int i=0; i<spinners.length; i++) spinners[i] = new entropySpinner();
> try { Thread.sleep(100); } catch (Exception e) { }
> for(int i=0; i<spinners.length; i++) {
> spinners[i].stop = true;
> randpool[i] = spinners[i].counter;
> }
>>But for some reason, under GCJ, the spinners keep spinning... the
>thread never stops. Could it be possible that GCJ is overaggressively
>optimizing out the while loop's test and turning it into an infinite
>loop?
>
Yes, GCJ doesn't yet understand "volatile" so its possible that its 
optimizing the test away, or even that the memory seen by two different 
CPUs is inconsistent (on an MP system). One work-around would be to put 
a synchronized {} around the test.
regards
Bryce.


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /