[m-rev.] diff: updates to extras/concurrency

Fergus Henderson fjh at cs.mu.OZ.AU
Sat Mar 1 04:06:11 AEDT 2003


On 28-Feb-2003, Peter Ross <pro at missioncriticalit.com> wrote:
> extras/concurrency/philo2.m:
> extras/concurrency/philo3.m:
> 	Add a C# version of rand_sleep.
...
> Index: philo2.m
> +:- pragma foreign_proc("C#", rand_sleep(Int::in, _IO0::di, _IO::uo),
> +		[thread_safe, will_not_call_mercury, promise_pure], "{
> +	System.Threading.Thread.Sleep((new System.Random()).Next(Int) * 1000);
>  }").
> Index: philo3.m
...
> +:- pragma foreign_proc("C#", rand_sleep(Int::in, _IO0::di, _IO::uo),
> +		[thread_safe, will_not_call_mercury, promise_pure], "{
> +	System.Threading.Thread.Sleep((new System.Random()).Next(Int) * 1000);
> +}").

The random numbers there are not properly random.
You should use the same System.Random value for each call,
e.g. by defining a class static variable to hold it.

	:- pragma foreign_code("C#", "
		static System.Random rng = new System.Random();
	").

	:- pragma foreign_proc("C#", rand_sleep(Int::in, _IO0::di, _IO::uo),
			[thread_safe, will_not_call_mercury, promise_pure], "{
		System.Threading.Thread.Sleep(rng.Next(Int) * 1000);
	}").

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list