/*
* barrier1.c */#include <stdio.h>
#include <unistd.h>#include <stdlib.h>#include <time.h>#include <pthread.h>#include <sys/neutrino.h>#include <timer.h>pthread_barrier_t barrier; // barrier synchronization object
void *thread1 (void *not_used)
{int policy;
struct sched_param param; pthread_getschedparam(pthread_self(),&policy,¶m); if(policy == SCHED_OTHER) printf("SCHED_OTHER 1 \n"); if(policy == SCHED_RR); printf("SCHED_RR 1 \n"); if(policy==SCHED_FIFO) printf("SCHED_FIFO 1 \n"); if(policy==SCHED_SPORADIC) printf("SCHED_SPARODIC 1 \n");setprio(0, 11); //设置线程优先级 越大优先级越高 0 代表当前进程
printf("thread1 priority is %d\n",getprio(0));time_t now;
time (&now);
printf ("thread1 starting at %s", ctime (&now));// do the computation
// let's just do a sleep here... while(1) { delay (100); pthread_barrier_wait (&barrier); // after this point, all three threads have completed. time (&now); printf ("barrier in thread1() done at %s", ctime (&now)); }}void *thread2 (void *not_used)
{ //查看线程的调度策略 默认为轮询 int policy; struct sched_param param; pthread_getschedparam(pthread_self(),&policy,¶m); if(policy == SCHED_OTHER) printf("SCHED_OTHER 2 \n"); if(policy == SCHED_RR); printf("SCHED_RR 2 \n"); if(policy==SCHED_FIFO) printf("SCHED_FIFO 2 \n"); if(policy==SCHED_SPORADIC) printf("SCHED_SPARODIC 1 \n");setprio(0, 10); //设置线程优先级 越大优先级越高 0 代表当前进程
printf("thread2 priority is %d\n",getprio(0));time_t now;
time (&now);
printf ("thread2 starting at %s", ctime (&now));// do the computation
// let's just do a sleep here... while(1) { delay (200); pthread_barrier_wait (&barrier); // after this point, all three threads have completed. time (&now); printf ("barrier in thread2() done at %s", ctime (&now)); }}int main () // ignore arguments
{pthread_t threadID1,threadID2;
setprio(0, 12); //设置线程优先级 越大优先级越高 0 代表当前进程
printf("Main priority is %d\n",getprio(0)); //CPU 时钟频率 struct _clockperiod timep; timep.nsec = 10*1000; timep.fract = 0; int ErrCode = 0; ErrCode = ClockPeriod(CLOCK_REALTIME, &timep, NULL, 0); if(ErrCode!=0) { printf( "Error: %s\n", strerror(ErrCode)); } /****************************************/ int ret = -1; int timer_interrupt_id = -1; ret = InitializeTimerInterrupt(0, 300, &timer_interrupt_id); //设置时钟中断,10ms /*****************************************/ time_t now;// create a barrier object with a count of 3
pthread_barrier_init (&barrier, NULL, 3);// start up two threads, thread1 and thread2
pthread_create (&threadID1, 0, thread1, 0); pthread_create (&threadID2, NULL, thread2, NULL); // at this point, thread1 and thread2 are running// now wait for completion
time (&now); printf ("main() waiting for barrier at %s", ctime (&now)); while(1) { InterruptWait(0, NULL); pthread_barrier_wait (&barrier); // after this point, all three threads have completed. time (&now); printf ("barrier in main() done at %s", ctime (&now)); } pthread_exit( NULL ); return (EXIT_SUCCESS);}