Re: SMP

From: Daniel A. Koepke (dkoepke@california.com)
Date: 08/15/99


Today, Fafhrd spake to the list:

>   Anyone have any tips on coding for SMP? We recently put Duris on a
> dual 400 board, and I was wondering how I'd take full advantage of
> this. I believe we need to specifically tell certain code to use the
> second processor? find_first_step() would be a prime candidate for
> that...

Multi-threading, such as that offered by POSIX threads (pthreads,
LinuxThreads, etc.).  There are several approaches you can take.  Pthreads
are fairly simple to use, but there's a lot of traps you can walk into
when the process has multiple threads of execution.  I'm not aware of any
resources on using POSIX threads, but the man pages are somewhat useful.

Something to get you started (Mailer Code),


    struct thread_data {
        pthread_t pid;
        pthread_mutex_t mut;
        void (*cntrl) (thread_data * td);
    };


    static void * _thread_stub (void * voidptr)
    {
        struct thread_data * td = (struct thread_data *) voidptr;
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

        (*thread->cntrl) (td);

        free(td);
        td = NULL;

        return (0);
    }


    struct thread_data * spawn_thread (void (*cntrl) (thread_data * td)) {
        pthread_attr_t attr;
        pthread_mutexattr_t mutAttr;
        struct thread_data * td;

        CREATE(td, struct thread_data, 1);

        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

        /* _np/_NP are non-POSIX extensions */
        pthread_mutexattr_init(&mutAttr);
        pthread_mutexattr_setkind_np(&mutAttr,PTHREAD_MUTEX_RECURSIVE_NP);

        pthread_mutex_init(&td->mut, &mutAttr);

        if (pthread_create(&td->pid, &attr, _thread_stub, td)) {
            perror("pthread_create");
            return (-1);
        }

        return (td);
    }

Also see pthread_mutex_lock, pthread_mutex_unlock, etc.

-dak


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST