[m-dev.] Is this a semaphore bug or comprehension failure on my part?

Fergus Henderson fjh at cs.mu.OZ.AU
Tue May 15 02:19:04 AEST 2001


On 14-May-2001, Ralph Becket <rbeck at microsoft.com> wrote:
> Another point is that if I recode main//0 as
> 
> main -->
>     semaphore__new(Fork0), semaphore__signal(Fork0),
>     semaphore__new(Fork1), semaphore__signal(Fork1),
>     semaphore__new(Fork2), semaphore__signal(Fork2),
>     semaphore__new(Fork3), semaphore__signal(Fork3),
>     semaphore__new(Fork4), semaphore__signal(Fork4),
>     spawn(philosopher("Plato",      0, Fork0, 1, Fork1)),
>     spawn(philosopher("Aristotle",  2, Fork2, 1, Fork1)),
>     spawn(philosopher("Descartes",  2, Fork2, 3, Fork3)),
>     spawn(philosopher("Calvin",     4, Fork4, 3, Fork3)),
>     spawn(philosopher("Hobbes",     4, Fork4, 0, Fork0)).
> 
> then the program terminates immediately, even though there are
> runnable threads lying around.  Shurely shome mishtake?

What grade are you running this in?

For grade hlc.gc.par, I think there is a bug in the current implementation
where if the main thread exits, then it calls exit() which will
terminate all the other threads.  This is alluded to in the following
comment in the code for ME_create_thread():

 |   int ME_create_thread(void *(*func)(void *), void *arg)
 |   {
 |         pthread_t       thread;
 | 
 |         if (pthread_create(&thread, MR_THREAD_ATTR, func, arg)) {
 |                 MR_fatal_error(""Unable to create thread."");
 |         }
 | 
 |         /*
 |         ** How do we ensure that the parent thread doesn't terminate until
 |         ** the child thread has finished it's processing?
 |         ** By the use of mutvars, or leave it up to user?
 |         */
 |         return TRUE;
 |   }

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list