From mboxrd@z Thu Jan 1 00:00:00 1970 To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> In-reply-to: Your message of "Fri, 30 May 2008 15:27:12 PDT." From: Bakul Shah Date: Fri, 30 May 2008 19:06:46 -0700 Message-Id: <20080531020646.B72395B4C@mail.bitblocks.com> Subject: Re: [9fans] simulation and newsqueak Topicbox-Message-UUID: b1555b12-ead3-11e9-9d60-3106f5b1d025 On Fri, 30 May 2008 15:27:12 PDT Skip Tavakkolian <9nut@9netics.com> wrote: > i need to build a monte carlo simulator to model a system. i > was first thinking of using libthread and channels, etc. but i'm > wondering if newsqueak would be a better fit. has it been used > for this? Why Newsqueak? Why not roll your own simulation kernel? Basically you need new_thread(function, argument, stackksize); condition = new_condition(); busy(how_many_time_units); wait(condition); signal(condition); yield(); // may be You do need stack switching code in asm or use setjmp/longjmp for it but the rest is pretty easy. The scheduler is a priority queue of (time-to-run,thread), each condition has a list of waiter threads, busy(n) puts the current thread on the schedule to run n time units in future, wait(cond) puts the current thread on the waiter list of cond, signal(cond) moves all waiter threads to the run-queue and yield()s etc. This simulation kernel is likely to be more efficient than a generic threads package. As an example, on a 1.5Ghz P4 (IBM T42) per thread context switch time is about 56ns for 1000 threads, increasing to about 490ns for 100,000 threads. You can use one or more threads to generate random events with whatever probabilistic distribution you want.