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, &param);

        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;
 }
/*运行结果:
前两秒主线程和第一个子线程交替运行,两秒后一直运行第二个子线程.*/
时间: 2024-11-09 03:02:06

thread_15的相关文章