[英]Pthreads stack priority
i have little trouble with pthread priority.I have set scheduler FIFO.我在 pthread 优先级方面没有什么问题。我已经设置了调度程序 FIFO。 I have 2 thread, each have diffretnt priority.
我有 2 个线程,每个线程都有不同的优先级。 On my project when i push A or B, one thread start working.
在我的项目中,当我按下 A 或 B 时,一个线程开始工作。 Thats fine, but when one thread working and push again B(priority15) and then A(priority 20) my assumption is that thread A jumps before B in stack queue and after first thread will by next thread A with priority 20 and then thread B (15).
那很好,但是当一个线程工作并再次推送 B(优先级 15)然后 A(优先级 20)时,我的假设是线程 A 在堆栈队列中的 B 之前跳转,并且在第一个线程之后将被下一个优先级为 20 的线程 A 然后线程 B (15)。 Is that problem FIFO or something else?
那个问题是先进先出还是别的什么? PS: I dont want use semaphore, i want to solve it just with priority and scheduler.
PS:我不想使用信号量,我只想用优先级和调度程序来解决它。 Thanks
谢谢
#include <stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
#include <time.h>
#include <sched.h>
sem_t mutex;
void* levelOnefunction(void *a)
{
sem_wait(&mutex);
int* b = (int *) a;
printf("Thread! next main!\n");
printf("sched prior:%d\n",*b);
sleep(3);
sem_post(&mutex);
return 0;
}
int main()
{
char s;
pthread_t t;
pthread_attr_t tattr;
struct sched_param param;
sem_init(&mutex,0,1);
pthread_attr_init(&tattr);
if(pthread_attr_setschedpolicy(&tattr,SCHED_FIFO)!=0)
printf("ERROR FIFO!\n");
//pthread_setschedparam(t,SCHED_FIFO,¶m);
if(pthread_attr_getschedparam(&tattr,¶m)!=0)
printf("ERROR attr get sheduler!\n");
/* int k =pthread_attr_setinheritsched(&tattr,PTHREAD_EXPLICIT_SCHED);
if (k!=0)
printf("ERROR\n"); */
printf("Initial priority is %d \n",param.sched_priority);
int min = sched_get_priority_min(SCHED_FIFO);
int max = sched_get_priority_max(SCHED_FIFO);
printf("MIN - %d ---> MAX - %d\n",min,max);
scanf("%c",&s);
while (s != '\0')
{
if(s=='a')
{
printf("a\n");
param.sched_priority=20;
if(pthread_attr_setschedparam(&tattr,¶m)!=0)
printf("ERROR attr_setschedul!\n");
printf("update priority is:%d\n",param.sched_priority);
if(pthread_create(&t,&tattr,levelOnefunction,(void *)¶m.sched_priority)!=0)
printf("ERROR thread1\n");
pthread_join(t,NULL);
printf("main finish!\n");
printf("%c end\n\n",s);
}
else if(s=='b')
{
printf("b\n");
param.sched_priority=15;
if(pthread_attr_setschedparam(&tattr,¶m)!=0)
printf("ERROR attr_setschedul!\n");
if(pthread_create(&t,&tattr,levelOnefunction,(void *)¶m.sched_priority)!=0)
printf("ERROR Thread 2 ");
pthread_join(t,NULL);
printf("main finish!\n");
printf("%c end\n\n",s);
}
else if(s=='\n')
{
}
else if (s!='a' && s!='b')
{
printf("bad key\n");
}
scanf("%c",&s);
}
sem_destroy(&mutex);
return 0;
}
You are calling pthread_join()
in the main thread immediately after creating each child thread.创建每个子线程后,您立即在主线程中调用
pthread_join()
。
This means that you never have more than one child thread created at a time - after creating one child thread, the main thread will then block in pthread_join()
until that child thread is complete.这意味着一次创建的子线程永远不会超过一个——在创建一个子线程后,主线程将阻塞在
pthread_join()
直到该子线程完成。 Only then does it call scanf()
again and potentially create another child thread.只有这样,它调用
scanf()
一次可能创造另一个子线程。
Thread priorities don't come into it.线程优先级不考虑在内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.