~ubuntu-branches/ubuntu/edgy/gnustep-base/edgy

« back to all changes in this revision

Viewing changes to Testing/diningPhilosophers.m

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2004-04-06 15:21:11 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040406152111-95pwmfi50qp1s594
Tags: 1.9.1-2
* debian/rules: Update CC.
* Source/NSUserDefaults: apply patch from CVS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
// NSLocks ... umm I mean forks
42
42
id forks[5];
 
43
id fork_lock;
43
44
 
44
45
//
45
46
// A class of hungry philosophers
69
70
        // Its a constant battle to feed yourself
70
71
        while (1)
71
72
        {
 
73
 
 
74
                [fork_lock lockWhenCondition:FOOD_SERVED];
 
75
 
72
76
                // Get the fork to our left
73
77
                [forks[chair] lockWhenCondition:FOOD_SERVED];
74
78
 
75
79
                // Get the fork to our right
76
80
                [forks[(chair + 1) % 5] lockWhenCondition:FOOD_SERVED];
77
81
 
 
82
                [fork_lock unlock];
 
83
 
78
84
                // Start eating!
79
 
                printf("Philosopher %d can start eating.\n", chair);
 
85
                printf("Philosopher %d can start eating.\n", chair); fflush(stdout);
80
86
 
81
87
                for (i = 0;i < 100000; ++i)
82
88
                {
83
89
                        if ((i % 10000) == 0)
84
 
                                printf("Philosopher %d is eating.\n", chair);
 
90
                                printf("Philosopher %d is eating.\n", chair); fflush(stdout);
85
91
                }
86
92
 
87
93
                // Done eating
88
 
                printf("Philosopher %d is done eating.\n", chair);
 
94
                printf("Philosopher %d is done eating.\n", chair); fflush(stdout);
89
95
 
90
96
                // Drop the fork to our left
91
97
                [forks[chair] unlock];
125
131
          [forks[i] lock];
126
132
  }
127
133
 
 
134
        fork_lock = [[NSConditionLock alloc]
 
135
                initWithCondition:NO_FOOD];
 
136
        [fork_lock lock];
 
137
 
128
138
  // Create the philosophers
129
139
  for (i = 0;i < 5; ++i)
130
140
          p[i] = [[Philosopher alloc] init];
135
145
                  toTarget:p[i] withObject: [NSNumber numberWithInt: i]];
136
146
 
137
147
  // Now let them all eat
 
148
        [fork_lock unlockWithCondition:FOOD_SERVED];
138
149
  for (i = 0;i < 5; ++i)
139
150
          [forks[i] unlockWithCondition:FOOD_SERVED];
140
151