From owner-freebsd-hackers Sun Jan 16 20:29:36 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from mail.nyct.net (bsd4.nyct.net [204.141.86.6]) by hub.freebsd.org (Postfix) with ESMTP id DAC1B14DA3 for ; Sun, 16 Jan 2000 20:29:32 -0800 (PST) (envelope-from mbac@nyct.net) Received: from bsd1.nyct.net (mbac@bsd1.nyct.net [204.141.86.3]) by mail.nyct.net (8.8.8/8.8.7) with ESMTP id XAA16920; Sun, 16 Jan 2000 23:29:31 -0500 (EST) (envelope-from mbac@nyct.net) Received: from localhost (mbac@localhost) by bsd1.nyct.net (8.8.8/8.9.3) with ESMTP id XAA00654; Sun, 16 Jan 2000 23:29:30 -0500 (EST) (envelope-from mbac@nyct.net) X-Authentication-Warning: bsd1.nyct.net: mbac owned process doing -bs Date: Sun, 16 Jan 2000 23:29:30 -0500 (EST) From: Michael Bacarella To: Alexander Litvin Cc: hackers@FreeBSD.ORG Subject: Re: Preemptiveness of FreeBSD threads In-Reply-To: <20000116225044.C601@unknown.nowhere.org> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > 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 > #include > #include > > #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