Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 16 Jan 2000 23:29:30 -0500 (EST)
From:      Michael Bacarella <mbac@nyct.net>
To:        Alexander Litvin <archer@lucky.net>
Cc:        hackers@FreeBSD.ORG
Subject:   Re: Preemptiveness of FreeBSD threads
Message-ID:  <Pine.BSF.4.05.10001162320540.179-100000@bsd1.nyct.net>
In-Reply-To: <20000116225044.C601@unknown.nowhere.org>

next in thread | previous in thread | raw e-mail | index | archive | help

> I'm kind of puzzled.

> Program, when killed with SIGINT, prints all counters and exits.

> Depending on the phase of the moon (it seems) sometimes my
> program gives (after ^C):

> This result is obtained for approximately the same runtime of the
> program. The same picture from 'ps'. I'm starting to beleive that
> the behaviour is dependent on the moon because two types of that
> behaviour seem to be changing not randomly, but rather go in periods:
> I can try for half an hour and receive the first, "wrong" result, and
> then something changes somewhere, and for another hour I get the
> second, "right" result.

> Now, is there something obvious, what I don't see?

> #include <pthread.h>
> #include <stdio.h>
> #include <signal.h>
> 
> #define THRNUM 10
> 
> pthread_t threads[THRNUM];
> int counters[THRNUM+1];

[snip]

This is just a wild guess, but the compiler might be optimizing the
increment loop. Since you haven't said anything is special about the
counters array, it's just going to assume normal circumstances, and in
fact, your increment loops probably generate to assembly as cpu register
increments.

Something abnormal does happen, though, and the compiler has no idea that
it can happen, so it never really writes the changes back to the counters
array. Hence, the counters array still shows all zeroes.

It's really dependant on the phase of the moon (or at least outside of my
understanding) when it will flush the values back into the array. Try
declaring:

int counters[THRNUM+1];

as

volatile int counters[THRNUM+1];

-MB





To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.05.10001162320540.179-100000>