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

QNX 线程 调度策略 优先级 时钟频率 同步的更多相关文章

  1. Linux 线程调度策略与线程优先级

    Linux内核的三种调度策略 SCHED_OTHER 分时调度策略. 它是默认的线程分时调度策略,所有的线程的优先级别都是0,线程的调度是通过分时来完成的.简单地说,如果系统使用这种调度策略,程序将无 ...

  2. Java多线程——线程的优先级和生命周期

    Java多线程——线程的优先级和生命周期 摘要:本文主要介绍了线程的优先级以及线程有哪些生命周期. 部分内容来自以下博客: https://www.cnblogs.com/sunddenly/p/41 ...

  3. C#夯实基础之多线程三:线程的优先级

    一.为什么需要优先级--线程调度的问题 在现实生活中,优先级是一个很常见的现象:在火车站,如果你是孕妇,你是可以走进站中的专门绿色通道的,可以提前上火车以免拥挤:火警119匪警110出警的时候,都是人 ...

  4. Linux-pthread如何设置线程的优先级

    设置线程优先级的函数: int pthread_setschedparam(pthread_t target_thread, int policy, const struct sched_param ...

  5. inux进程/线程调度策略与 进程优先级

    目的: 系统性的认识linux的调度策略(SCHED_OTHER.SCHED_FIFO.SCHED_RR): 实时调度?分时调度? 混搭系统(实时任务+分时任务),怎样调度. linux的调度策略 l ...

  6. Java并发编程之线程生命周期、守护线程、优先级、关闭和join、sleep、yield、interrupt

    Java并发编程中,其中一个难点是对线程生命周期的理解,和多种线程控制方法.线程沟通方法的灵活运用.这些方法和概念之间彼此联系紧密,共同构成了Java并发编程基石之一. Java线程的生命周期 Jav ...

  7. C#编程 线程,任务和同步(2) 开启线程

    创建线程的几种方法: 1 异步委托 创建线程的一种简单方式是定义一个委托,并异步调用它. 委托是方法的类型安全的引用.Delegate类 还支持异步地调用方法.在后台,Delegate类会创建一个执行 ...

  8. 线程安全、数据同步之 synchronized 与 Lock

    本文Demo下载传送门 写在前面 本篇文章讲的东西都是Android开源网络框架NoHttp的核心点,当然线程.多线程.数据安全这是Java中就有的,为了运行快我们用一个Java项目来讲解. 为什么要 ...

  9. java 22 - 6 多线程之线程调度和设置线程的优先级

    线程调度 假如我们的计算机只有一个 CPU,那么 CPU 在某一个时刻只能执行一条指令,线程只有得到 CPU时间片,也就是使用权,才可以执行指令. 那么Java是如何对线程进行调用的呢? 线程有两种调 ...

随机推荐

  1. Java 如何判断一个字符是否是数字或字母

    在C++中, 可以用isdigit()判断一个字符是否是数字,可以用isalpha()判断一个字符是否是字母,还有很多,都在<cctype>头文件中 而类似的方法在JAVA中,则主要是Ch ...

  2. 服务器端开发(Python/C++)-今日头条-拉勾网-最专业的互联网招聘平台

    服务器端开发(Python/C++)-今日头条-拉勾网-最专业的互联网招聘平台 服务器端开发(Python/C++)

  3. hdu4405概率dp入门

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 10_9 java笔记

    java中所有的关键字都是小写的注意main虽然被编译器识别,但是它并不是关键字包:(名字小写) 单级包:liyi 多级包:cn.itcast path 和classpath的区别:path环境变量里 ...

  5. json接口相关(建议结合JFinal框架)

    /** * */ package net.wicp.wvqusrtg; import java.util.HashMap; import net.sf.json.JSONArray; import n ...

  6. Hibernate 笔记1

    Hibernate表generator标签的作用,如下图,

  7. Android 中 关闭部分 Activity 的方法总结

    每一个activity都有自己的生命周期,被打开了最终就要被关闭. 四种结束当前的activity方法 但如果已经启动了四个Activity:A,B,C和D, 在D Activity里,想再启动一个A ...

  8. 一种用javascript实现的比较兼容的回到顶部demo + 阻止事件冒泡

    回到页面顶部demo <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  9. 2014 HDU多校弟六场J题 【模拟斗地主】

    这是一道5Y的题目 有坑的地方我已在代码中注释好了 QAQ Ps:模拟题还是练的太少了,速度不够快诶 //#pragma comment(linker, "/STACK:16777216&q ...

  10. 创建出多个app

    修改红线里的内容