Linux Threads

C programming in Linux

UNIX System Calls and Subroutines using C

generic pthread programming

status of pthread_join

//prototype 
// int pthread_create(pthread_t * thread, pthread_attr_t * attr, void * (*start_routine)(void *), void * arg);
// int pthread_join(pthread_t th, void **thread_return);
// void pthread_exit(void *value_ptr);
// #define PTHREAD_CANCELED ((void *) -1)
 
void *thread_run(void *data){
    ptr = (Event* ) data;
    //work on event
    ...
 
        pthread_exit((void*)ptr);
}
 
void main()
{
    Event  event(....)
    pthread_create(&thread_tid, attr, thread_run, (void *)(&event));
 
        .....
 
    void * status;
    int s = pthread_join(thread_tid, &status);
    if (s != 0)
        handle_error_en(s, "pthread_join");
 
    if (status == PTHREAD_CANCELED)
        printf("Thread was canceled.");
    else
    {
        //status will be a point to Event;
        // so could then be like:
        // Event * ptr= (Event *) status; 
    }
 
}

pthread_cleanup_push()/pthread_cleanup_pop()

sample threadpool

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License