线程的属性。
#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);
}