电子文章 | 电子资料下载 | 家电维修 | 维修资料下载 | 加入收藏 | 全站地图
您现在所在位置:电子爱好者电子文章EDA/PLD线程解析(二)

线程解析(二)

11-20 00:29:46 | http://www.5idzw.com | EDA/PLD | 人气:282
标签:eda技术,eda技术实用教程,http://www.5idzw.com 线程解析(二),http://www.5idzw.com

  作者:曹忠明,www.5idzw.com嵌入式学院讲师。

  上次说了如何去创建一个线程及如何向线程中传递一个参数那么这次我们说一下如何终止一个线程。

  如进程中调用exit,_exit使进程结束一样,线程也可以通过类似的方法结束。
        一、线程的执行体结束线程自然结束。
        二、可以通过调用pthread_exit使程序结束。

  pthread_exit 的原型为:
        void pthread_exit(void *value_ptr);
        value_ptr是一个无类型返回值,其他线程可以通过调用phread_join来获得这个值。
        phread_join 的原型为:
        int pthread_join(pthread_t thread, void **value_ptr);

  调用pthread_join可以等待线程thread的退出并获得退出时的状态,如果不关心线程返回值的话,value_ptr可以置为NULL,下面我们用一个例程说明这两个函数的使用方法。
        #include <stdio.h>
        #include <pthread.h>

void *thread_a(void *arg)
        {
                printf("thread 1 enter\n");
                pthread_exit((void *)1);
        }

void *thread_b(void *arg)
        {
                printf("thread 2 enter\n");
                pthread_exit((void *)2);
        }

int main(int argc, char **argv)
        {
                pthread_t tid_a,tid_b;
                int err;
                void *value_ptr;

        err = pthread_create(&tid_a,NULL,thread_a,NULL);
                if(err < 0)
               {
                        perror("pthread_create thread_a");
                }

        err = pthread_create(&tid_b,NULL,thread_b,NULL);
                if(err < 0)
                {
                        perror("pthread_create thread_a");
                }

        pthread_join(tid_b,&value_ptr);
                printf("phtread %d exit!\n",(int)value_ptr);

        pthread_join(tid_a,&value_ptr);
                printf("phtread %d exit!\n",(int)value_ptr);

        sleep(5);
                printf("the main close\n");
                return 0;
        }

  三、被统一线程中的其他线程取消而结束。

  线程可以通过调用pthread_cancel函数向同一进程中的其他线程发送取消的信号,但是这个先好的响应可以设定,可以设置为立即终止或忽略。所以发送取消信号并不意味着线程就会终止。

  与线程取消相关的函数有:
        phtread_cancel原型为:
        int pthread_cancel(pthread_t thread);
        这个函数向线程thread发送终止信号。
        pthread_setcancelstate原型为:
        int pthread_setcancelstate(int state, int *oldstate);

  这个函数设定线程接收到终止信号的反应,state有两种值:PHTREAD_CANCEL_ENABLE(默认为这个状态)和PHREAD_CANCEL_DISABLE,这两个值分别代表接受掉终止信号终止线程和或略这个信号。oldstate用来存放线程原来的状态用来以后恢复只用,如不用回复可以设置为NULL。

pthread_setcanceltype原型为:
        int pthread_setcanceltype(int type, int *oldtype);

  这个函数用来设定线程接收到终止信号的执行时间,type也是有两个值:PTHREAD_CANCEL_DEFFERED和PTHREAD_CANCEL_ASYCHRONOUS,这两个值分别表示线程接收到终止信号是运行到下一个取消点退出还是立即退出。同样oldtype也是用来存放线程原来的状态。

pthread_testcancel原型为:
        void pthread_testcancel(void);
        检查线程是否处于canceld状态如果是则执行取消动作否则立即返回。

  下面用一个例程说明这几个函数的使用。
        #include <stdio.h>
        #include <pthread.h>

void *thread_a(void *arg)
        {
                if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL) != 0)
                {

[1] [2] [3]  下一页

,线程解析(二)
关于《线程解析(二)》的更多文章