当前位置: 技术问答>linux和unix
【linux菜鸟第一帖】:请问多线程开发,为何不能设置多个线程的优先级问题?
来源: 互联网 发布时间:2016-11-24
本文导语: 代码如下: pthread_t id_encode; pthread_t id_decode; pthread_attr_t attr; struct sched_param param; int newprio; newprio=25; pthread_attr_init(&attr); pthread_attr_setschedpolicy(&attr,SCHED_RR); pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM); ...
代码如下:
pthread_t id_encode;
pthread_t id_decode;
pthread_attr_t attr;
struct sched_param param;
int newprio;
newprio=25;
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr,SCHED_RR);
pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);
pthread_attr_getschedparam(&attr, ¶m);
param.sched_priority=newprio;
pthread_attr_setschedparam(&attr, ¶m);
ret = pthread_create(&id_encode,&attr,(void*)encode_thread,NULL);
if ( 0 != ret )
{
trace( "create encode_thread thread errorn");
goto RELEASE;
}
pthread_attr_destroy(&attr);
printf("1.create encode_thread ok!n");
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr,SCHED_RR);
pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);
newprio=17;
pthread_attr_getschedparam(&attr, ¶m);
param.sched_priority=newprio;
pthread_attr_setschedparam(&attr, ¶m);
ret = pthread_create(&id_decode,&attr,(void*)decode_thread,NULL);
if ( 0 != ret )
{
trace( "create decode_thread thread errorn");
goto RELEASE;
}
pthread_attr_destroy(&attr);
printf("2.create decode_thread ok!n");
运行之后,在超级终端的输出信息,只有: 1.create encode_thread ok! , 然后感觉程序就死掉了?系统也没有继续往下面跑?..
不知道我在创建第二个线程的时候,有关线程属性参数的设置有什么问题?? 难道是优先级的设置不对吗???
pthread_t id_encode;
pthread_t id_decode;
pthread_attr_t attr;
struct sched_param param;
int newprio;
newprio=25;
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr,SCHED_RR);
pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);
pthread_attr_getschedparam(&attr, ¶m);
param.sched_priority=newprio;
pthread_attr_setschedparam(&attr, ¶m);
ret = pthread_create(&id_encode,&attr,(void*)encode_thread,NULL);
if ( 0 != ret )
{
trace( "create encode_thread thread errorn");
goto RELEASE;
}
pthread_attr_destroy(&attr);
printf("1.create encode_thread ok!n");
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr,SCHED_RR);
pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);
newprio=17;
pthread_attr_getschedparam(&attr, ¶m);
param.sched_priority=newprio;
pthread_attr_setschedparam(&attr, ¶m);
ret = pthread_create(&id_decode,&attr,(void*)decode_thread,NULL);
if ( 0 != ret )
{
trace( "create decode_thread thread errorn");
goto RELEASE;
}
pthread_attr_destroy(&attr);
printf("2.create decode_thread ok!n");
运行之后,在超级终端的输出信息,只有: 1.create encode_thread ok! , 然后感觉程序就死掉了?系统也没有继续往下面跑?..
不知道我在创建第二个线程的时候,有关线程属性参数的设置有什么问题?? 难道是优先级的设置不对吗???
|
简单的测试了下,你看下面的代码:
运行结果
//pthread2.c
#include
#include
//#include
void encode_thread()
{
}
void decode_thread()
{
}
int main(){
pthread_t id_encode;
pthread_t id_decode;
pthread_attr_t attr;
struct sched_param param;
int newprio;
int ret;
newprio=25;
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr,SCHED_RR);
pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);
pthread_attr_getschedparam(&attr, ¶m);
param.sched_priority=newprio;
pthread_attr_setschedparam(&attr, ¶m);
ret = pthread_create(&id_encode,&attr,(void*)encode_thread,NULL);
if ( 0 != ret )
{
fprintf(stderr, "create encode_thread thread errorn");
return -1;
}
pthread_attr_destroy(&attr);
printf("1.create encode_thread ok!n");
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr,SCHED_RR);
pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);
newprio=17;
pthread_attr_getschedparam(&attr, ¶m);
param.sched_priority=newprio;
pthread_attr_setschedparam(&attr, ¶m);
ret = pthread_create(&id_decode,&attr,(void*)decode_thread,NULL);
if ( 0 != ret )
{
fprintf(stderr, "create decode_thread thread errorn");
return -1;
}
pthread_attr_destroy(&attr);
printf("2.create decode_thread ok!n");
return 0;
}
运行结果
$gcc pthread2.c -lpthread
$./a.out
1.create encode_thread ok!
2.create decode_thread ok!
|
管道的容量限制不影响你的数据传输吧
写入时,如果管道已满,那就阻塞
读进程从管道中读取数据之后,管道有剩余空间,写进程就可以继续写入了
写入时,如果管道已满,那就阻塞
读进程从管道中读取数据之后,管道有剩余空间,写进程就可以继续写入了