• <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);
            初始化一個(gè)線程屬性對象.
            pthread_destroy
            回收一個(gè)線程屬性對象.

            #include <pthread.h>
            int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
            int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
            這個(gè)屬性允許我們無需對線程進(jìn)行合并:
            | PTHREAD_CREATE_JOINABLE 默認(rèn).
            | PTHREAD_CREATE_DETACHED 不能調(diào)用pthread_join來獲得另一個(gè)線程的退出狀態(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 默認(rèn)
            | SCHED_RP   循環(huán)(round_robin) <- 下面2個(gè)調(diào)度方式具備實(shí)時(shí)調(diào)度功能,需要root權(quán)限.
            | SCHED_FIFO 先進(jìn)先出

            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);
            控制一個(gè)線程調(diào)度的計(jì)算方式,目前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在實(shí)現(xiàn)線程時(shí),默認(rèn)的棧很大,這個(gè)屬性有點(diǎn)多余.

            創(chuàng)建脫離線程:
            /*
             * 線程屬性-創(chuàng)建脫離線程
             * 主線程不等待子線程結(jié)束,只通過thread_finished標(biāo)志來檢測子線程是否已結(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è)計(jì)

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

            導(dǎo)航

            統(tǒng)計(jì)

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            99久久久精品免费观看国产| 久久免费香蕉视频| 精品永久久福利一区二区| 欧美久久精品一级c片片| 久久精品中文字幕第23页| 久久亚洲电影| 99久久精品国内| 亚洲精品NV久久久久久久久久| 亚洲精品国产自在久久| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 国产91久久精品一区二区| 久久九九有精品国产23百花影院| 国产精品一区二区久久不卡| 一本色道久久88加勒比—综合| 久久九色综合九色99伊人| 久久丫忘忧草产品| 99久久亚洲综合精品网站| 精品国产99久久久久久麻豆| 伊人久久大香线焦综合四虎| 麻豆成人久久精品二区三区免费| 欧美精品福利视频一区二区三区久久久精品| 亚洲国产精品综合久久一线| 国产99精品久久| 久久香蕉国产线看观看精品yw| 伊人精品久久久久7777| 91精品婷婷国产综合久久| 伊人久久综合精品无码AV专区| 久久99精品九九九久久婷婷| 久久亚洲AV成人无码国产 | 久久综合久久久| 久久久久99精品成人片试看 | 久久99国产精品成人欧美| 亚洲中文字幕久久精品无码APP| 色诱久久av| 国产精品美女久久久久AV福利| 国内精品久久久久| 潮喷大喷水系列无码久久精品| 亚洲中文字幕无码一久久区| 国产精品熟女福利久久AV| 国产成人精品久久亚洲| 精品久久人人做人人爽综合|