linux程序設計筆記--pthread--attributes

線程的屬性。
 #include<pthread.h>
 int pthread_attr_init(pthread_attr_t*attr);
線程屬性初始化。
 
 
設置線程爲脫離線程。
 intpthread_attr_setdetachstate(pthread_attr_t *attr,intdetachstate);
 pthread_attr_t *attr
:線程屬性指針。
 intdetachstate
:線程屬性標誌:PTHREAD_CREAT_JOIN,PTHREAD_CREAT_DETACHED.
 
線程屬性標誌缺省值爲PTHREAD_CREAT_JOIN及歸併線程。PTHREAD_CREAT_DETACHED設置線程爲脫離線程。
 設置線程調度策略。
 intpthread_attr_setschedpolicy(pthread_attr_t *attr,int policy)

綜合例程:

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <pthread.h>



void *thread_function(void *arg);



char message[] = "Hello World";

int thread_finished = 0;



int main() {

     int res;

    pthread_t a_thread;

    void *thread_result;

    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 *)message);

    if (res != 0) {

        perror("Thread creation failed");

        exit(EXIT_FAILURE);

    }


(void)pthread_attr_destroy(&thread_attr);

    while(!thread_finished) {

        printf("Waiting for thread to say it'sfinished...\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 exitingnow\n");

    thread_finished = 1;

    pthread_exit(NULL);

}


8,取消一個線程。
 #include<pthread.h>
 int pthread_cancle(pthread_t thread);

 int pthread_setcanclestate(int state,int*oldstate); //設置線程的取消狀態。
 intstate
:如果爲PTHREAD_CANCLE_ENABLE,允許線程接受取消請求。
 
如果爲PTHREAD_CANCEL_DISABLE,不允許線程接受取消請求,屏蔽取消請求。
 int*oldstate
:獲取線程以前的取消狀態,如果不需要,設置爲NULL
 int pthread_setcancletype(int type,int*oldtype); //設置取消類型。
 inttype
:如果爲PTHREAD_CANCLE_ASYNCHRONOUS,接到請求立馬執行取消操作。
   
如果爲PTHREAD_CANCLE_DEFERRED,執行取消操作之前,先執行以下函數之一:pthread_join,pthread_cond_wait,
   pthead_cond_tomewait,pthread_testcancle,sem_waith,sigwait

 int*oldtype
:索引以前的狀態,如果不需要,直接爲NULL
 
在缺省狀態下,線程啓動時的取消狀態爲PTHEAD_CANCLE_ENABLE,取消類型爲PTHREAD_CANCLE_DEFERRED.


#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#include <pthread.h>


#define NUM_THREADS 6


void *thread_function(void *arg);


int main() {

    int res;

    pthread_t a_thread[NUM_THREADS];

    void *thread_result;

    int lots_of_threads;

    for(lots_of_threads = 0; lots_of_threads < NUM_THREADS;lots_of_threads++) {

        res = pthread_create(&(a_thread[lots_of_threads]), NULL,thread_function, (void *)lots_of_threads);

        if (res != 0) {

            perror("Thread creation failed");

            exit(EXIT_FAILURE);

        }

        /* sleep(1); */

    }



    printf("Waiting for threads to finish...\n");

    for(lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0;lots_of_threads--) {

        res = pthread_join(a_thread[lots_of_threads], &thread_result);

         if (res == 0) {

            printf("Picked up a thread\n");

        } else {

            perror("pthread_join failed");

        }

    }


    printf("All done\n");

    exit(EXIT_SUCCESS);

}


void *thread_function(void *arg) {

    int my_number = (int)arg;

    int rand_num;



    printf("thread_function is running. Argument was %d\n",my_number);

    rand_num=1+(int)(9.0*rand()/(RAND_MAX+1.0));

    sleep(rand_num);

    printf("Bye from %d\n", my_number);

    pthread_exit(NULL);

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章