unix 多线程控制应用
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,1.线程三个属性的学习绑定分离优先级thread.c#include"thread.h"#include#include#include#include#includeextern int statu
千家信息网最后更新 2025年02月01日unix 多线程控制应用
1.线程三个属性的学习
绑定
分离
优先级
thread.c
- #include"thread.h"
- #include<stdio.h>
- #include<unistd.h>
- #include<pthread.h>
- #include<stdbool.h>
- #include<stdlib.h>
- extern int status_1;
- extern int status_2;
- void thread_0(char*pstr)//带参数
- {
- int counter = 10;
- while(counter--)
- {
- sleep(1);
- printf("%s\n",pstr);
- }
- pthread_exit(&status_1);//线程退出专用函数
- }
- void thread_1(void)//不带参数
- {
- int counter = 10;
- while(counter--)
- {
- sleep(1);
- printf("thread_1\n");
- }
- pthread_exit(&status_2);//线程退出专用函数
- }
- bool _creat_thread(pthread_t *thread, void *(*start_routine)(void *), void *arg, int SCOPE, int DETACH, int policy, int priority_val)
- {
- struct sched_param param;
- pthread_attr_t attr;
- if(0 != pthread_attr_init(&attr))//初始化 结构体attr, 如果不需要attr需要取消attr, pthread_attr_destroy
- {
- perror("pthread_attr_init\n");
- return false;
- }
- if(0 != pthread_attr_setscope(&attr, SCOPE))//设置线程属性 是否绑定 绑定(PTHREAD_SCOPE_SYSTEM) 非绑定(PTHREAD_SCOPE_PROCESS)
- {
- perror("pthread_attr_setscope\n");
- return false;
- }
- if(0 != pthread_attr_setdetachstate(&attr, DETACH))//设置线程属性 是否分离 分离(PTHREAD_CREATE_DETACHED) 非分离(PTHREAD_CREATE_JOINABLE)
- {
- perror("pthread_attr_setdetachstate\n");
- return false;
- }
- if(0 != pthread_attr_setschedpolicy(&attr, policy))//三种优先级策略选择 : SCHED_FIFO(值1-99), SCHED_RR(值1-99), and SCHED_OTHER
- {
- perror("pead_attr_setschedpolicy\n");
- return false;
- }
- if(priority_val>0 && priority_val<100)//判断优先级值是否在1-99范围
- {
- param.sched_priority = priority_val;
- }
- else
- {
- perror("priority_val_ wrong value range!!\n");
- }
- if(0 != pthread_attr_setschedparam(&attr, ¶m))//设置设置线程属性 优先级 通过 策略与值来判断如何调度
- {
- perror("pthread_attr_setschedparam\n");
- return false;
- }
- if(0 != pthread_create(thread, &attr, (void*)start_routine,arg))// 建立一个含有以上属性的线程
- {
- perror("pthread_create\n");
- return false;
- }
- if(0 != pthread_attr_destroy(&attr))//使用完后取消attr,
- {
- perror("pthread_attr_destroy\n");
- return false;
- }
- return true;
- }
thread.h
- #ifndef THREAD_H
- #define THREAD_H
- #include<stdio.h>
- #include<pthread.h>
- #include<stdbool.h>
- void thread_0(char*);
- void thread_1(void);
- bool _creat_thread(pthread_t *,void *(*)(void *), void *, int, int, int, int);
- #endif //end THREAD_H
main.c
- #include"thread.h"
- #include<pthread.h>
- #include<unistd.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<stdbool.h>
- #define LOW_PRIO 1
- #define HIGH_PRIO 2
- int status_0 = 0;
- int status_1 = 1;
- int status_2 = 2;
- int main(void)
- {
- bool ret;
- char *str = "thread_0\n";
- pthread_t thd0,thd1;
- ret = _creat_thread(&thd0, (void*)thread_0, str, PTHREAD_SCOPE_SYSTEM, PTHREAD_CREATE_JOINABLE, SCHED_FIFO, LOW_PRIO);// 创建一个线程 带参数级相应属性
- if(ret)
- {
- printf("create thread successfully!\n");
- }
- else
- {
- perror("fail to create thread!\n");
- exit(1);
- }
- ret = _creat_thread(&thd1, (void*)thread_1, NULL, PTHREAD_SCOPE_SYSTEM, PTHREAD_CREATE_JOINABLE, SCHED_FIFO, HIGH_PRIO);// 创建一个线程 不带参数,含相应属性
- if(ret)
- {
- printf("create thread successfully!\n");
- }
- else
- {
- perror("fail to create thread!\n");
- exit(1);
- }
- int * thd_exit_status = NULL ;
- while(1)
- {
- sleep(1);
- printf("main\n");
- if(0 != pthread_join(thd0,(void**)&thd_exit_status))//if th0 no more exist, pthread_join return erro and the thd_exit_status = NULL
- {
- printf("thread_0 is not exist!!\n");
- //exit(1);
- }
- else
- {
- if(NULL== thd_exit_status && 1 != *thd_exit_status)
- {
- printf("pthd0 is runing\n");
- }
- else if(NULL!= thd_exit_status && 1 == *thd_exit_status)
- {
- printf("pthd0 has been terminated and return status:%d\n", *thd_exit_status);
- }
- }
- if(0 != pthread_join(thd1,(void**)&thd_exit_status))//if th0 no more exist, pthread_join return erro and the thd_exit_status = NULL
- {
- printf("thread_1 is not exist!!\n");
- //exit(1);
- }
- else
- {
- if(NULL == thd_exit_status && 2 != *thd_exit_status)
- {
- printf("pthd_1 is runing\n");
- }
- else if(NULL!= thd_exit_status && 2 == *thd_exit_status)
- {
- printf("pthd_1 has been terminated and return status:%d\n", *thd_exit_status);
- }
- }
- }
- return 0;
- }
线程
属性
优先级
参数
函数
策略
专用
三个
结构
范围
附件
学习
调度
选择
多线
应用
控制
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
bgp跨线路云服务器
计算机网络安全文献在线观看
珠海仿真软件开发
网络安全法主题班会
电力新闻网络安全预警单
注册一家网络技术公司地址
数据库技术基础试卷
编制网络安全预算相关文件
智能科技互联网平台
池州租房软件开发
怎么在阿里云购买香港的服务器
数据库中的数据表索引
服务器怎么在bios里查看硬盘
小米打电话显示服务器错误
数据库中表与表的关系
临沂巨久网络技术公司
网络安全个人该怎么做
数据库设计专业好吗
怎么导出数据库MySQL
云服务器是什么时候兴起的
软件开发工作分类有几种类型
手机定位服务器地址
数据库统计出现次数最多的算法
怎么把域名绑定到服务器
多串口服务器生产
泰拉瑞亚国服服务器联机
linux服务器外网访问
软件开发类型的工作职位
移动通信网络安全征文
无人机架构软件开发