Ping qznc: Re: A little of coordination for Rosettacode

Brian Rogoff brogoff at gmail.com
Sat Jun 22 11:53:30 PDT 2013


On Saturday, 16 February 2013 at 11:30:00 UTC, Jos van Uden wrote:
> On 16-2-2013 8:58, qznc wrote:
>> On Saturday, 16 February 2013 at 06:58:01 UTC, qznc wrote:
>>> On Saturday, 16 February 2013 at 02:23:42 UTC, Jos van Uden 
>>> wrote:
>>>> On 5-2-2013 20:45, Jos van Uden wrote:
>>>>
>>>>> By the way, I think 'Qznc' may want to have a look at 'The 
>>>>> dining
>>>>> philosophers':
>>>>>
>>>>> http://rosettacode.org/wiki/Dining_philosophers
>>>
>>> I should find the time to solve it this weekend.
>>
>> Wow, my kid let me do some hacking right now and it was 
>> simpler than expected.
>> Posted a solution already.
>
> Wow, that was quick. Thanks!

The current D code for Dining philosophers does not compile with 
dmd v2.063.2, the error message being

dining.d(34): Error: cannot uniquely infer foreach argument types

The code looks OK to me (but I'm a D newbie) so I wonder if 
someone could explain the inference issue. Where should an 
annotation be added to allow this to compile?


      1	import std.stdio, std.algorithm, std.string, 
std.parallelism,
      2	  core.sync.mutex;
      3	
      4	void eat(in uint i, in string name, Mutex[] forks) {
      5	  writeln(name, " is hungry.");
      6	
      7	  immutable j = (i + 1) % forks.length;
      8	
      9	  // Take forks i and j. The lower one first to prevent 
deadlock.
     10	  auto fork1 = forks[min(i, j)];
     11	  auto fork2 = forks[max(i, j)];
     12	
     13	  fork1.lock();
     14	  scope(exit) fork1.unlock();
     15	
     16	  fork2.lock();
     17	  scope(exit) fork2.unlock();
     18	
     19	  writeln(name, " is eating.");
     20	  writeln(name, " is full.");
     21	}
     22	
     23	void think(in string name) {
     24	  writeln(name, " is thinking.");
     25	}
     26	
     27	void main() {
     28	  const philosophers = "Aristotle Kant Spinoza Marx 
Russell".split();
     29	  Mutex[philosophers.length] forks;
     30	  foreach (ref fork; forks)
     31	    fork = new Mutex();
     32	
     33	  defaultPoolThreads = forks.length;
     34	  foreach (uint i, philo; taskPool.parallel(philosophers)) 
{
     35	    foreach (_; 0 .. 100) {
     36	      eat(i, philo, forks);
     37	      think(philo);
     38	    }
     39	  }
     40	}

BTW, I like the coding style being used in the rosetta examples 
and TDPL much better than the library style.

-- Brian





More information about the Digitalmars-d-learn mailing list