Advanced Programming in UNIX Environment Episode 51

#include "apue.h"
#include <pthread.h>

void cleanup(void *arg)
{
    printf("cleanup: %s\n", (char *) arg);
}

void * thr_fn1(void *arg)
{
    printf("thread 1 start\n");
    pthread_cleanup_push(cleanup,"thread 1 first handler");
    pthread_cleanup_push(cleanup,"thread 1 second handler");
    printf("thread 1 push complete\n");
    if(arg)
        return (void*)1;
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    return (void*)1;
}

void * thr_fn2(void *arg)
{
    printf("thread 2 start\n");
    pthread_cleanup_push(cleanup,"thread 2 first handler");
    pthread_cleanup_push(cleanup,"thread 2 second handler");
    printf("thread 2 push complete\n");
    if(arg)
        pthread_exit((void *)2);
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    pthread_exit((void *)2);
}

int main(void)
{
    int err;
    pthread_t tid1,tid2;
    void *tret;

    err=pthread_create(&tid1,NULL,thr_fn1, (void *)1);
    if(err!=0)
        err_exit(err,"can't create thread 1");
    err=pthread_create(&tid2,NULL,thr_fn1, (void *)2);
    if(err!=0)
        err_exit(err,"can't create thread 2");
    err=pthread_join(tid1,&tret);
    if(err!=0)
        err_exit(err,"can't join with thread 1");
    printf("thread 1 exit code %ld\n", (long)tret);
    err=pthread_join(tid2,&tret);
    if(err!=0)
        err_exit(err,"can't join with thread 2");
    printf("thread 2 exit code %ld\n", (long)tret);
    return 0;
}

Thread cleanup handler

By default, a thread’s termination status is retained until we call pthread_join for that thread. A thread’s underlying storage can be reclaimed immediately on termination if the thread has been detached. After a thread is detached, we can’t use the pthread_join function to wait for its termination status, because calling pthread_join for a detached thread results in undefined behavior. We can detach a thread by calling pthread_detach.

#include <pthread.h>

int pthread_detach(pthread_t tid);

Thread Synchronization

When multiple threads of control share the same memory, we need to make sure that each thread sees a consistent view of its data. If each thread uses variables that other threads don’t read or modify, no consistency problems will exist. Similarly, if a variable is read-only, there is no consistency problem with more than one thread reading its value at the same time. However, when one thread can modify a variable that other threads can read or modify, we need to synchronize the threads to ensure that they don’t use an invalid value when accessing the variable’s memory contents.

We also need to synchronize two or more threads that might try to modify the same variable at the same time. Consider the case in which we increment a variable (Figure 11.9). The increment operation is usually broken down into three steps.

1.Read the memory location into a register.
2.Increment the value in the register.
3.Write the new value back to the memory location.

上一篇:zabbix监控windows下的mysql


下一篇:Ninth season twelfth episode,Phoebe fed a bunch of rats!!!!!!