
我的电脑有以下特点 –
# chrt -m SCHED_OTHER min/max priority : 0/0 SCHED_FIFO min/max priority : 1/99 SCHED_RR min/max priority : 1/99 SCHED_BATCH min/max priority : 0/0 SCHED_IDLE min/max priority : 0/0
这是程序 –
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <iostream> /** get pID **/ #include <sys/types.h> #include <unistd.h> /** kill signal **/ #include <signal.h> using namespace std; int shared_variable = 0; class helium_thread { private: pthread_t *thread_ID; pID_t process_pID; public: static pthread_mutex_t mutex_thread; static pthread_cond_t cond_var; voID set_thread_ID(pthread_t tID); pthread_t *get_thread_ID(); int create_thread(pthread_t *thread_ptr,const pthread_attr_t *attr,voID * (*start_routine)(voID *),voID *arg ); helium_thread(); ~helium_thread(); }; helium_thread thread_1,thread_2; /** The deFinition of the static member can't be insIDe a function,You need to put it outsIDe **/ /** When I trIEd using insIDe a function,I got the error - error: invalID use of qualified-name 'helium_thread::mutex_thread **/ pthread_mutex_t helium_thread::mutex_thread = PTHREAD_MUTEX_INITIAliZER; pthread_cond_t helium_thread::cond_var = PTHREAD_COND_INITIAliZER; voID helium_thread::set_thread_ID( pthread_t tID) { *(this->thread_ID) = tID; } pthread_t * helium_thread::get_thread_ID( ) { return (this->thread_ID); } int helium_thread::create_thread(pthread_t *thread_ptr,voID *arg ) { int ret; ret = pthread_create(thread_ptr,attr,start_routine,(voID *)arg) ; return ret; } helium_thread::helium_thread() { thread_ID = new pthread_t; } helium_thread::~helium_thread() { delete thread_ID; } /** While defining the methods of the class,Keywords static and virtual should not be repeated in the deFinition. **/ /** They should only be used in the class declaration. **/ /** remember this **/ #if 0 enum schedparam_policy { SCHED_RR,SCHED_IO,SCHED_FIFO,SCHED_OTHER }; struct sched_param { int sched_priority; /* Scheduling priority */ }; #endif /*** End here ***/ voID print_sched_priority() { int rc; struct sched_param sched_param; int policy; rc = pthread_getschedparam(pthread_self(),&policy,&sched_param); if( rc != 0) { cout<<"Getting Scheduling paramter Failed"<<endl; } else { cout<<"The priority of Thread_Func_A is "<< sched_param.sched_priority<<endl; const char *policy_name = (policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER":"??"; cout<<"The policy of Thread_Func_A is "<< policy_name<<endl; cout<<"The max priority is "<<sched_get_priority_max(policy)<<endl; cout<<"The min priority is "<<sched_get_priority_min(policy)<<endl; } } voID set_sched_priority() { int rc; struct sched_param sched_param; int policy; policy = SCHED_RR; /** highest priority **/ sched_param.sched_priority = 0; /** with chrt -m or sched_get_priority_max,I get to kNow the priority range **/ rc = pthread_setschedparam(pthread_self(),policy,&sched_param); if( rc != 0) { cout<<"Setting Scheduling paramter Failed"<<endl; } else { cout<<"The priority of Thread_Func_A is "<< sched_param.sched_priority<<endl; const char *policy_name = (policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER":"??"; cout<<"The policy of Thread_Func_A is "<< policy_name<<endl; } } voID *Thread_Function_A(voID *thread_arg) { print_sched_priority(); set_sched_priority(); print_sched_priority(); #if 0 while(shared_variable < 10) { /** print only odd numbers **/ pthread_mutex_lock(&(helium_thread::mutex_thread)); if ( (shared_variable % 2 ) == 0 ) { /** It means the even number; this thread should not print even numbers **/ pthread_cond_wait(&helium_thread::cond_var,&helium_thread::mutex_thread); } cout<<"Thread A shared_variable ..odd = "<< shared_variable<<endl; shared_variable++; pthread_mutex_unlock(&(helium_thread::mutex_thread)); } #endif pthread_exit(NulL); } voID thread_b_cleanup (voID* buffer) { cout<<"Clean up for thread_b"<<endl; delete[] (int *)buffer; } voID *Thread_Function_B(voID *thread_arg) { voID *thread_b_resource = new int[5]; pthread_cleanup_push (thread_b_cleanup,thread_b_resource); while(shared_variable < 10) { pthread_mutex_lock(&(helium_thread::mutex_thread)); if ( (shared_variable % 2 ) != 0 ) { /** This means odd **/ pthread_cond_signal (&helium_thread::cond_var); } else { cout<<"Thread B prints shared_variable--even = "<< shared_variable<<endl; shared_variable++; } pthread_mutex_unlock(&(helium_thread::mutex_thread)); } pthread_cleanup_pop (1); pthread_exit(NulL); } int main(int argc,char *argv[]) { pID_t thread_pID_val = getpID(); thread_1.create_thread((thread_1.get_thread_ID()),NulL,Thread_Function_A,&thread_pID_val); //thread_2.create_thread((thread_2.get_thread_ID()),Thread_Function_B,&thread_pID_val); pthread_join( *(thread_1.get_thread_ID()),NulL); //pthread_join( *(thread_2.get_thread_ID()),NulL); return 0; }
输出如下 –
# ./thread_basic.out The priority of Thread_Func_A is 0 The policy of Thread_Func_A is SCHED_OTHER The max priority is 0 The min priority is 0 Setting Scheduling paramter Failed The priority of Thread_Func_A is 0 The policy of Thread_Func_A is SCHED_OTHER The max priority is 0 The min priority is 0
调度的设置失败。 我能做些什么来解决它?
将服务器的输出redirect到客户端套接字
unix – 文件中有多less条logging和字符出现次数的细分
如何克隆远程linux服务器上存在的git仓库到windows
Eclipse CDT不能识别<iostream>
path内文件夹内的文件内部的小写字母
linux wget没有通过authentication?
Git如何知道要推送哪个存储库?
我如何在linux中使用COM ATL项目?
R stats – 分配大型matrix/ linux时的内存问题
按计划重新启动AWS EC2实例上的Tomcat服务
pthread_setschedparam(3)通过返回0成功,并通过返回错误代码(如errno(3))并适用于strerror(3)失败。 所以代码
rc = pthread_setschedparam(pthread_self(),&sched_param); if( rc != 0) { cerr<<"Setting Scheduling paramter Failed rc="<<rc << " " << strerror(rc) << endl; }
找出原因。 请仔细阅读sched_setscheduler(2) ,特别是它的NOTES部分…
根据pthread_setschedparam(3)的man页, EINVAL表示:
EINVAL :政策不是公认的政策,或参数对政策没有意义。
总结以上是内存溢出为你收集整理的为什么在我的程序中设置预定的策略和参数失败 – pthread Linux?全部内容,希望文章能够帮你解决为什么在我的程序中设置预定的策略和参数失败 – pthread Linux?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)