• <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 線程屬性 脫離線程

            線程的屬性

            脫離線程:不向主線程返回信息,不需要主線程等待.
            通過兩種方法創(chuàng)建:
            調(diào)用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 不能調(diào)用pthread_join來獲得另一個線程的退出狀態(tài).

            int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
            int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
            控制線程的調(diào)度方式:
            | SCHED_OTHER 默認
            | SCHED_RP   循環(huán)(round_robin) <- 下面2個調(diào)度方式具備實時調(diào)度功能,需要root權(quán)限.
            | 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線程的調(diào)度策略.

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

            int pthread_attr_setscope(pthread_attr_t *attr, int scope);
            int pthread_attr_getscope(const pthread_attr_t *attr, int *scope);
            控制一個線程調(diào)度的計算方式,目前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);
            控制線程創(chuàng)建的棧大小,單位字節(jié).可選.
            Linux在實現(xiàn)線程時,默認的棧很大,這個屬性有點多余.

            創(chuàng)建脫離線程:
            /*
             * 線程屬性-創(chuàng)建脫離線程
             * 主線程不等待子線程結(jié)束,只通過thread_finished標志來檢測子線程是否已結(jié)束,并顯示線程之間仍然共享的變量.
             
            */
            #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);
            }

            執(zhí)行結(jié)果:
            $ 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程序設(shè)計

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

            導(dǎo)航

            統(tǒng)計

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久超乳爆乳中文字幕| 久久午夜电影网| 一本一本久久aa综合精品| 精品人妻伦九区久久AAA片69| 亚洲精品乱码久久久久久久久久久久 | 2020最新久久久视精品爱| 久久99精品久久久久久| 性做久久久久久免费观看| 97精品依人久久久大香线蕉97| 91精品国产乱码久久久久久| 久久精品无码一区二区app| 午夜精品久久久久久影视riav| 久久影院综合精品| 青草影院天堂男人久久| 久久亚洲国产成人影院网站| 99精品国产99久久久久久97| 国产成人精品久久二区二区| 国产激情久久久久影院小草 | 99久久久精品免费观看国产| 久久久久99精品成人片三人毛片 | 国产亚洲精品美女久久久| 精品久久久久中文字幕一区| 精品久久久久久国产| 精品免费tv久久久久久久| 性高朝久久久久久久久久| 久久精品a亚洲国产v高清不卡| 久久精品无码一区二区三区日韩| 99久久国产精品免费一区二区 | 久久久久国产精品三级网| 99久久国产亚洲综合精品| 久久久一本精品99久久精品88| 久久国产免费观看精品| 久久久久青草线蕉综合超碰| 国产精品99久久99久久久| 亚洲国产精品久久久天堂| 久久久久久噜噜精品免费直播| 久久精品a亚洲国产v高清不卡| 狠狠色丁香婷婷久久综合| 久久激情五月丁香伊人| 日韩亚洲欧美久久久www综合网| 亚洲日本va中文字幕久久|