Processing Math: Done
No jsMath TeX fonts found -- using unicode fonts instead.
This may be slow and might not print well.
Use the jsMath control panel to get additional information.
jsMath Control PanelHide this Message


jsMath
Differences between revisions 2 and 6 (spanning 4 versions)
Revision 2 as of 2007-01-21 03:26:29
Size: 2347
Editor: wstein
Comment:
Revision 6 as of 2008-11-14 13:42:00
Size: 2708
Editor: anonymous
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
    ctypedef int pthread_t # actual type will be used by Pyrex below...     ctypedef int pthread_t # actual type will be used by Pyrex
Line 16: Line 16:
    void pthread_exit(void *value_ptr)     void pthread_exit(void *value_pt)
    int pthread_attr_init(pthread_attr_t *)
    int pthread_attr_destroy(pthread_attr_t *)
    int pthread_attr_setdetachstate(pthread_attr_t *,int)
    int pthread_join(pthread_t ,void **)

    cdef enum:
       PTHREAD_CREATE_JOINABLE
Line 22: Line 29:
Here's an example:

Illustrating calling into interesting C libraries, etc.

Here's an example (you can paste this into foo.spyx, load it, and call go(10)):

Add pthread Support to SageX

Make it possible to use C-level POSIX threads from SageX programs.

1. Create a pxi file with the pthread.h functions declared.

  • Here's a start:

cdef extern from "pthread.h":
    ctypedef int pthread_t       # actual type will be used by Pyrex 
    ctypedef int pthread_attr_t     
    int pthread_create(pthread_t *thread, 
                       pthread_attr_t *attr,
                       void *(*start_routine)(void *), 
                       void *arg)
    int pthread_join(pthread_t thread, void **value_ptr)
    void pthread_exit(void *value_pt)
    int pthread_attr_init(pthread_attr_t *)
    int pthread_attr_destroy(pthread_attr_t *)
    int pthread_attr_setdetachstate(pthread_attr_t *,int)
    int pthread_join(pthread_t ,void **)

    cdef enum:
       PTHREAD_CREATE_JOINABLE

The file should be nicely documented, etc.

2. Create spyx files that illustrate how to use various threading constructs

Illustrating calling into interesting C libraries, etc.

Here's an example (you can paste this into foo.spyx, load it, and call go(10)):

cdef extern from "pthread.h":
    ctypedef int pthread_t       # actual type will be used by Pyrex below...
    ctypedef int pthread_attr_t     
    int pthread_create(pthread_t *thread, 
                       pthread_attr_t *attr,
                       void *(*start_routine)(void *), 
                       void *arg)
    int pthread_join(pthread_t thread, void **value_ptr)
    void pthread_exit(void *value_ptr)

cdef extern from "unistd.h":
    unsigned int sleep(unsigned int seconds)


cdef void *PrintHello(void *thread_id):
   printf("\n%d: Hello World...", thread_id)
   sleep(1)
   printf("Thread %d ending\n", thread_id)
   pthread_exit(NULL)

def go(int n):
    cdef pthread_t* threads
    threads = <pthread_t*> malloc(sizeof(pthread_t) * n)
    cdef int rc, t
    for t from 0 <= t < n:
        printf("Creating thread %d...", t)
        rc = pthread_create(&threads[t], NULL, PrintHello, <void*> t)
        if rc:
            printf("ERROR; return code from pthread_create() is %d\n", rc)
            return
    print('done creating all threads; now waiting')
    for t from 0 <= t < n:
        pthread_join(threads[t], <void**> NULL)
    free(threads)
    print('done freeing all memory used to store thread data.')


cdef int n
cdef void *PrintDot(void* thread_id):
    global n
    while 1:
        printf(".. %d ..\n", n)
        sleep(3)
        n += 1

def print_dot():
    cdef pthread_t t
    pthread_create(&t, NULL, PrintDot, <void*> NULL)

def nval():
    print n

msri07/pthread_sagex (last edited 2008-11-14 13:42:00 by anonymous)