#include <pthread.h> int pthread_attr_init(pthread_attr_t *attr);还有一个回收函数pthread_attr_destroy,它的目的是对属性对象进行清理和回收.一旦对象被回收了,除非它被重新初始化,否则就不能被再次利用.
/*************************************************************************
> File Name: thread5.c
> Description: thread5.c程序创建一个线程属性thread_attr,并将其设置为脱离状态,然后创建一个新线程,设置属性为thread_attr.
> Author: Liubingbing
> Created Time: 2015年07月06日 星期一 20时36分36秒
> Other: thread5.c程序与之前程序的不同在于,原来的线程不再等待与它创建的新线程合并.主线程通过thread_finish标志来检测新线程是否结束.
************************************************************************/
#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;
/* 线程属性对象thread_attr */
pthread_attr_t thread_attr;
/* pthread_attr_init初始化一个线程属性对象 */
res = pthread_attr_init(&thread_attr);
if (res != 0) {
perror("Attribute creation failed");
exit(EXIT_FAILURE);
}
/* pthread_attr_setdetachstate设置属性,PTHREAD_CREATE_DETACHED不允许调用pthread_join获得另一个线程的退出状态 */
res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
if (res != 0) {
perror("Setting detached attribute failed");
exit(EXIT_FAILURE);
}
/* pthread_create创建一个新线程
* 第一个参数athread中存放新线程的标识符
* 第二个参数thread_attr为线程属性,上面的代码就是设置这个对象
* 第三个参数thread_function为新线程将要调用的函数
* 第四个参数(void *)message为传递给将要调用函数的参数 */
res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);
if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
/* pthread_attr_destroy函数对线程属性对象thread_attr进行清理和回收 */
(void)pthread_attr_destroy(&thread_attr);
/* 等待新线程函数设置thread_finished=1 */
while (!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 exiting now\n");
thread_finished = 1;
pthread_exit(NULL);
}
这个程序有两段比较重要的代码,第一段代码是:pthread_attr_t thread_attr; res = pthread_attr_init(&thread_attr);它表明了一个线程属性并对其进行初始化,第二段代码是:
res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);它把属性的值设置为脱离状态.
/*************************************************************************
> File Name: thread6.c
> Description: thread6.c程序设置线程的调度属性
> Author: Liubingbing
> Created Time: 2015年07月06日 星期一 21时15分10秒
> Other: thread6.c程序先通过sched_get_priority获取允许的优先级范围,然后设置优先级为min_priority
************************************************************************/
#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;
int max_priority;
int min_priority;
struct sched_param scheduling_value;
/* 线程属性对象thread_attr */
pthread_attr_t thread_attr;
/* pthread_attr_init初始化一个线程属性对象 */
res = pthread_attr_init(&thread_attr);
if (res != 0) {
perror("Attribute creation failed");
exit(EXIT_FAILURE);
}
/* pthread_attr_setdetachstate设置属性,PTHREAD_CREATE_DETACHED不允许调用pthread_join获得另一个线程的退出状态 */
res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
if (res != 0) {
perror("Setting detached attribute failed");
exit(EXIT_FAILURE);
}
/* pthread_attr_setschedpolicy设置调度策略*/
res = pthread_attr_setschedpolicy(&thread_attr, SCHED_OTHER);
if (res != 0) {
perror("Setting scheduling policy failed");
exit(EXIT_FAILURE);
}
/* 查找可用的优先级的范围 */
max_priority = sched_get_priority_max(SCHED_OTHER);
min_priority = sched_get_priority_min(SCHED_OTHER);
/* 设置优先级 */
scheduling_value.sched_priority = min_priority;
res = pthread_attr_setschedparam(&thread_attr, &scheduling_value);
if (res != 0) {
perror("Seting scheduling priority failed");
exit(EXIT_FAILURE);
}
/* pthread_create创建一个新线程
* 第一个参数athread中存放新线程的标识符
* 第二个参数thread_attr为线程属性,上面的代码就是设置这个对象
* 第三个参数thread_function为新线程将要调用的函数
* 第四个参数(void *)message为传递给将要调用函数的参数 */
res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);
if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
/* pthread_attr_destroy函数对线程属性对象thread_attr进行清理和回收 */
(void)pthread_attr_destroy(&thread_attr);
/* 等待新线程函数设置thread_finished=1 */
while (!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 exiting now\n");
thread_finished = 1;
pthread_exit(NULL);
}
这个程序与设置脱离状态属性很相似,区别只是设置的是调度策略.版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/yiranant/article/details/46779311