外贸网站建设商家,网页的建设,沈阳网站关键字优化,wordpress 卡密注册在下面的代码片段中#xff0c;我创建了6个线程。各有不同的优先级。全局优先级数组中提到了优先级。我正在根据线索索引在每个线程内连续增加全局变量。如果线程优先级更高#xff0c;我期待计数更高。但我的输出不遵循优先概念pl。请参阅下面显示的输出顺序。我在Ubuntu 16…在下面的代码片段中我创建了6个线程。各有不同的优先级。全局优先级数组中提到了优先级。我正在根据线索索引在每个线程内连续增加全局变量。如果线程优先级更高我期待计数更高。但我的输出不遵循优先概念pl。请参阅下面显示的输出顺序。我在Ubuntu 16.04和Linux内核4.10上尝试了这一点。Linux线程优先级行为不正常O/P,Thread0Thread3Thread2Thread5Thread1Thread4pid32155 count4522138740pid32155 count4509082289pid32155 count4535088439pid32155 count4517943246pid32155 count4522643905pid32155 count4519640181代码#include #include #define FAILURE -1#define MAX_THREADS 15long int global_count[MAX_THREADS];/* priority of each thread */long int priority[]{1,20,40,60,80,99};void clearGlobalCounts(){int i0;for(i0;iglobal_count[i]0;}/**thread parameter is thread index**/void funcDoNothing(void *threadArgument){int count0;int index *((int *)threadArgument);printf(Thread%d\n,index);clearGlobalCounts();while(1){count;if(count100){global_count[index];count0;}}}int main(){int i0;for(int i0;icreate_thread(funcDoNothing, i,priority[i]);sleep(3600);for(i0;i{printf(pid%d count%ld\n,getpid(),global_count[i]);}}create_thread(void *func,int thread_index,int priority){pthread_attr_t attr;struct sched_param schedParam;void *pParmNULL;int id;int * index malloc(sizeof(int));*index thread_index;void *res;/* Initialize the thread attributes */if (pthread_attr_init(attr)){printf(Failed to initialize thread attrs\n);return FAILURE;}if(pthread_attr_setschedpolicy(attr, SCHED_FIFO)){printf(Failed to pthread_attr_setschedpolicy\n);return FAILURE;}if (pthread_attr_setschedpolicy(attr, SCHED_FIFO)){printf(Failed to setschedpolicy\n);return FAILURE;}/* Set the capture thread priority */pthread_attr_getschedparam(attr, schedParam);;schedParam.sched_priority sched_get_priority_max(SCHED_FIFO) - 1;schedParam.sched_priority priority;if (pthread_attr_setschedparam(attr, schedParam)){printf(Failed to setschedparam\n);return FAILURE;}pthread_create(id, attr, (void *)func, index);}