首页 > 代码库 > thread_15
thread_15
#include <stdlib.h>#include <pthread.h>#include <stdio.h>#include <sched.h>#include <sched.h>void *consumer(void *p){ do{ printf("<<<<(%u),%d\n", (unsigned)pthread_self(), (unsigned int)p); }while(1);}int main(int argc, char *argv[]) { pthread_t t1, t2, t3; int ret; struct sched_param param; param.sched_priority = 1; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&attr, SCHED_RR); pthread_attr_setschedparam(&attr, ¶m); ret = pthread_create(&t1, NULL, consumer,(void *)1); sleep(2); ret = pthread_create(&t2, &attr, consumer,(void *)2); if(ret != 0) { printf("create failed,%d\n", ret); exit(1); } pthread_attr_destroy(&attr); sleep(1); printf("main returned\n"); return 0; }/*运行结果:前两秒主线程和第一个子线程交替运行,两秒后一直运行第二个子线程.*/
thread_15
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。