• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            小默

            thread 線程屬性 脫離線程

            線程的屬性

            脫離線程:不向主線程返回信息,不需要主線程等待.
            通過兩種方法創建:
            調用pthread_detach;
            修改線程的屬性. <- 這里使用

            #include <pthread.h>
            int pthread_attr_init(pthread_attr_t *attr);
            初始化一個線程屬性對象.
            pthread_destroy
            回收一個線程屬性對象.

            #include <pthread.h>
            int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
            int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
            這個屬性允許我們無需對線程進行合并:
            | PTHREAD_CREATE_JOINABLE 默認.
            | PTHREAD_CREATE_DETACHED 不能調用pthread_join來獲得另一個線程的退出狀態.

            int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
            int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
            控制線程的調度方式:
            | SCHED_OTHER 默認
            | SCHED_RP   循環(round_robin) <- 下面2個調度方式具備實時調度功能,需要root權限.
            | SCHED_FIFO 先進先出

            int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);
            int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param);
            和schedpolicy配合使用,控制SCHED_OTHER線程的調度策略.

            int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit);
            int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit);
            | PTHREAD_EXPLICIT_SCHED 調度由屬性明確設置.
            | PTHREAD_INHERIT_SCHED  新線程沿用創建者的屬性.

            int pthread_attr_setscope(pthread_attr_t *attr, int scope);
            int pthread_attr_getscope(const pthread_attr_t *attr, int *scope);
            控制一個線程調度的計算方式,目前Linux只支持PTHREAD_SCOPE_SYSTEM.

            int pthread_attr_setstacksize(pthread_attr_t *attr, int scope);
            int pthread_attr_getstacksize(const pthread_attr_t *attr, int *scope);
            控制線程創建的棧大小,單位字節.可選.
            Linux在實現線程時,默認的棧很大,這個屬性有點多余.

            創建脫離線程:
            /*
             * 線程屬性-創建脫離線程
             * 主線程不等待子線程結束,只通過thread_finished標志來檢測子線程是否已結束,并顯示線程之間仍然共享的變量.
             
            */
            #include 
            <stdio.h>
            #include 
            <unistd.h>
            #include 
            <stdlib.h>
            #include 
            <pthread.h>

            void *thread_function(void *arg);

            char g_message[] = "Hello World!";
            int g_thread_finished = 0;

            int main(){
                
            int res;
                pthread_t a_thread;
                pthread_attr_t thread_attr;

                res 
            = pthread_attr_init(&thread_attr);
                
            if(res != 0){
                    perror(
            "Attribute creation failed");
                    exit(EXIT_FAILURE);
                }

                res 
            = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
                
            if(res != 0){
                    perror(
            "Setting detached attribute failed");
                    exit(EXIT_FAILURE);
                }

                res 
            = pthread_create(&a_thread, &thread_attr,
                        thread_function, (
            void *)g_message);
                
            if(res != 0){
                    perror(
            "Thread creation failed");
                    exit(EXIT_FAILURE);
                }

                (
            void)pthread_attr_destroy(&thread_attr);
                
            while(!g_thread_finished){
                    printf(
            "Waiting for thread to say it's finished\n");
                    sleep(
            1);
                }
                printf(
            "Other thread finished, bye!\n");
                exit(EXIT_SUCCESS);
            }

            void *thread_function(void *arg){
                printf(
            "thread_function is running. Argument was %s\n", (char *)arg);
                sleep(
            4);
                printf(
            "Second thread setting finished flag, and exit now\n");
                g_thread_finished 
            = 1;
                pthread_exit(NULL);
            }

            執行結果:
            $ gcc -D_REENTRANT thread5.c -o thread5 -lpthread  
            $ .
            /thread5 
            Waiting 
            for thread to say it's finished
            thread_function is running. Argument was Hello World!
            Waiting 
            for thread to say it's finished
            Waiting for thread to say it's finished
            Waiting for thread to say it's finished
            Second thread setting finished flag, and exit now
            Other thread finished, bye
            !

            --
            FROM: Linux程序設計

            posted on 2011-06-14 18:56 小默 閱讀(2240) 評論(0)  編輯 收藏 引用 所屬分類: Linux

            導航

            統計

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久精品无码专区免费青青| 一本久久久久久久| 久久久久久国产a免费观看黄色大片 | 久久天天躁狠狠躁夜夜不卡| 久久久久亚洲精品日久生情 | 久久久综合香蕉尹人综合网| 久久99这里只有精品国产| 久久久久亚洲AV片无码下载蜜桃| 久久亚洲AV成人无码电影| 国产精品日韩欧美久久综合| 99久久精品免费看国产一区二区三区 | 伊人久久大香线焦AV综合影院 | 青草国产精品久久久久久| 久久se精品一区二区| 热RE99久久精品国产66热| 欧美黑人又粗又大久久久| 久久久久久久久久免免费精品| 无码国内精品久久人妻蜜桃| 国内精品久久久久久不卡影院| 色婷婷综合久久久中文字幕| 久久精品这里只有精99品| 99久久久精品免费观看国产| 一级做a爰片久久毛片免费陪| 97精品伊人久久久大香线蕉| 久久Av无码精品人妻系列| 久久青青草视频| 一级做a爰片久久毛片免费陪| 精品久久无码中文字幕| 久久久久亚洲AV成人片| 精品国产乱码久久久久久人妻| 久久综合九色综合久99| 久久涩综合| 久久久久国产亚洲AV麻豆| 国产日韩久久久精品影院首页| 久久夜色tv网站| 久久WWW免费人成—看片| 99热精品久久只有精品| 中文字幕亚洲综合久久2| 精品久久久久久久久久久久久久久| 久久99精品国产99久久6男男| 国产精品久久久久jk制服|