QNX 多线程 (线程1每隔20ms读取 number;线程2每隔10ms计算一次)

#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#define MAX 10

pthread_t thread[2];
pthread_mutex_t mut;
int number=0,i;

void *thread1()
{
while(1)
{
number=i;
printf ("Thread1: number=%d\n",number);
delay(20);
}
}

void *thread2()
{
for (i = 0; i < MAX*100; i++)
{
delay(10);
}
pthread_exit(NULL);
}

int main()
{
clock_t start,end;
start=clock();
pthread_create(&thread[0], NULL, thread1, NULL);
pthread_create(&thread[1], NULL, thread2, NULL);
pthread_join(thread[0],NULL);
pthread_join(thread[1],NULL);
end=clock();
printf("The time is %lf s!\n",(double)((end-start)/CLOCKS_PER_SEC));
return 0;
}

结果显示:

Thread1: number=0
Thread1: number=1
Thread1: number=3
Thread1: number=5
Thread1: number=7
Thread1: number=9
Thread1: number=11
Thread1: number=12
Thread1: number=14
Thread1: number=16
Thread1: number=18
Thread1: number=20
Thread1: number=22
Thread1: number=23
Thread1: number=25
Thread1: number=27
Thread1: number=29
Thread1: number=31
Thread1: number=33
Thread1: number=34
Thread1: number=36
Thread1: number=38
Thread1: number=40
Thread1: number=42
Thread1: number=44
Thread1: number=45
Thread1: number=47
Thread1: number=49
Thread1: number=51
Thread1: number=53
Thread1: number=55
Thread1: number=56
Thread1: number=58
Thread1: number=60
Thread1: number=62
Thread1: number=64
Thread1: number=66
Thread1: number=67
Thread1: number=69
Thread1: number=71
Thread1: number=73
Thread1: number=75
Thread1: number=77
Thread1: number=78
Thread1: number=80
Thread1: number=82
Thread1: number=84
Thread1: number=86
Thread1: number=88
Thread1: number=89
Thread1: number=91
Thread1: number=93
Thread1: number=95
Thread1: number=97
Thread1: number=99
Thread1: number=100
Thread1: number=102
Thread1: number=104
Thread1: number=106
Thread1: number=108
Thread1: number=110
Thread1: number=111
Thread1: number=113
Thread1: number=115
Thread1: number=117
Thread1: number=119
Thread1: number=121
Thread1: number=122
Thread1: number=124
Thread1: number=126
Thread1: number=128
Thread1: number=130
Thread1: number=132
Thread1: number=133
Thread1: number=135
Thread1: number=137
Thread1: number=139
Thread1: number=141
Thread1: number=143
Thread1: number=144
Thread1: number=146
Thread1: number=148
Thread1: number=150
Thread1: number=152
Thread1: number=154
Thread1: number=155
Thread1: number=157
Thread1: number=159
Thread1: number=161
Thread1: number=163
Thread1: number=165
Thread1: number=166
Thread1: number=168
Thread1: number=170
Thread1: number=172
Thread1: number=174
Thread1: number=176
Thread1: number=177
Thread1: number=179
Thread1: number=181
Thread1: number=183
Thread1: number=185
Thread1: number=187
Thread1: number=188
Thread1: number=190
Thread1: number=192
Thread1: number=194
Thread1: number=196
Thread1: number=198
Thread1: number=199
Thread1: number=201
Thread1: number=203
Thread1: number=205
Thread1: number=207
Thread1: number=209
Thread1: number=210
Thread1: number=212
Thread1: number=214
Thread1: number=216
Thread1: number=218
Thread1: number=220
Thread1: number=221
Thread1: number=223
Thread1: number=225
Thread1: number=227
Thread1: number=229
Thread1: number=231
Thread1: number=232
Thread1: number=234
Thread1: number=236
Thread1: number=238
Thread1: number=240
Thread1: number=242
Thread1: number=243
Thread1: number=245
Thread1: number=247
Thread1: number=249
Thread1: number=251
Thread1: number=253
Thread1: number=254
Thread1: number=256
Thread1: number=258
Thread1: number=260
Thread1: number=262
Thread1: number=264
Thread1: number=265
Thread1: number=267
Thread1: number=269
Thread1: number=271
Thread1: number=273
Thread1: number=275
Thread1: number=276
Thread1: number=278
Thread1: number=280
Thread1: number=282
Thread1: number=284
Thread1: number=286
Thread1: number=287
Thread1: number=289
Thread1: number=291
Thread1: number=293
Thread1: number=295
Thread1: number=297
Thread1: number=298
Thread1: number=300
Thread1: number=302
Thread1: number=304
Thread1: number=306
Thread1: number=308
Thread1: number=309
Thread1: number=311
Thread1: number=313
Thread1: number=315
Thread1: number=317
Thread1: number=319
Thread1: number=320
Thread1: number=322
Thread1: number=324
Thread1: number=326
Thread1: number=328
Thread1: number=330
Thread1: number=331
Thread1: number=333
Thread1: number=335
Thread1: number=337
Thread1: number=339
Thread1: number=341
Thread1: number=342
Thread1: number=344
Thread1: number=346
Thread1: number=348
Thread1: number=350
Thread1: number=352
Thread1: number=353
Thread1: number=355
Thread1: number=357
Thread1: number=359
Thread1: number=361
Thread1: number=363
Thread1: number=364
Thread1: number=366
Thread1: number=368
Thread1: number=370
Thread1: number=372
Thread1: number=374
Thread1: number=375
Thread1: number=377
Thread1: number=379
Thread1: number=381
Thread1: number=383
Thread1: number=385
Thread1: number=386
Thread1: number=388
Thread1: number=390
Thread1: number=392
Thread1: number=394
Thread1: number=396
Thread1: number=397
Thread1: number=399
Thread1: number=401
Thread1: number=403
Thread1: number=405
Thread1: number=407
Thread1: number=408
Thread1: number=410
Thread1: number=412
Thread1: number=414
Thread1: number=416
Thread1: number=418
Thread1: number=419
Thread1: number=421
Thread1: number=423
Thread1: number=425
Thread1: number=427
Thread1: number=429
Thread1: number=430
Thread1: number=432
Thread1: number=434
Thread1: number=436
Thread1: number=438
Thread1: number=440
Thread1: number=441
Thread1: number=443
Thread1: number=445
Thread1: number=447
Thread1: number=449
Thread1: number=451
Thread1: number=452
Thread1: number=454
Thread1: number=456
Thread1: number=458
Thread1: number=460
Thread1: number=462
Thread1: number=463
Thread1: number=465
Thread1: number=467
Thread1: number=469
Thread1: number=471
Thread1: number=473
Thread1: number=474
Thread1: number=476
Thread1: number=478
Thread1: number=480
Thread1: number=482
Thread1: number=484
Thread1: number=485
Thread1: number=487
Thread1: number=489
Thread1: number=491
Thread1: number=493
Thread1: number=495
Thread1: number=496
Thread1: number=498
Thread1: number=500
Thread1: number=502
Thread1: number=504
Thread1: number=506
Thread1: number=507
Thread1: number=509
Thread1: number=511
Thread1: number=513
Thread1: number=515
Thread1: number=517
Thread1: number=518
Thread1: number=520
Thread1: number=522
Thread1: number=524
Thread1: number=526
Thread1: number=528
Thread1: number=529
Thread1: number=531
Thread1: number=533
Thread1: number=535
Thread1: number=537
Thread1: number=539
Thread1: number=540
Thread1: number=542
Thread1: number=544
Thread1: number=546
Thread1: number=548
Thread1: number=550
Thread1: number=551
Thread1: number=553
Thread1: number=555
Thread1: number=557
Thread1: number=559
Thread1: number=561
Thread1: number=562
Thread1: number=564
Thread1: number=566
Thread1: number=568
Thread1: number=570
Thread1: number=572
Thread1: number=573
Thread1: number=575
Thread1: number=577
Thread1: number=579
Thread1: number=581
Thread1: number=583
Thread1: number=584
Thread1: number=586
Thread1: number=588
Thread1: number=590
Thread1: number=592
Thread1: number=594
Thread1: number=595
Thread1: number=597
Thread1: number=599
Thread1: number=601
Thread1: number=603
Thread1: number=605
Thread1: number=606
Thread1: number=608
Thread1: number=610
Thread1: number=612
Thread1: number=614
Thread1: number=616
Thread1: number=617
Thread1: number=619
Thread1: number=621
Thread1: number=623
Thread1: number=625
Thread1: number=627
Thread1: number=628
Thread1: number=630
Thread1: number=632
Thread1: number=634
Thread1: number=636
Thread1: number=638
Thread1: number=639
Thread1: number=641
Thread1: number=643
Thread1: number=645
Thread1: number=647
Thread1: number=649
Thread1: number=650
Thread1: number=652
Thread1: number=654
Thread1: number=656
Thread1: number=658
Thread1: number=660
Thread1: number=661
Thread1: number=663
Thread1: number=665
Thread1: number=667
Thread1: number=669
Thread1: number=671
Thread1: number=672
Thread1: number=674
Thread1: number=676
Thread1: number=678
Thread1: number=680
Thread1: number=682
Thread1: number=683
Thread1: number=685
Thread1: number=687
Thread1: number=689
Thread1: number=691
Thread1: number=693
Thread1: number=694
Thread1: number=696
Thread1: number=698
Thread1: number=700
Thread1: number=702
Thread1: number=704
Thread1: number=705
Thread1: number=707
Thread1: number=709
Thread1: number=711
Thread1: number=713
Thread1: number=715
Thread1: number=716
Thread1: number=718
Thread1: number=720
Thread1: number=722
Thread1: number=724
Thread1: number=726
Thread1: number=727
Thread1: number=729
Thread1: number=731
Thread1: number=733
Thread1: number=735
Thread1: number=737
Thread1: number=738
Thread1: number=740
Thread1: number=742
Thread1: number=744
Thread1: number=746
Thread1: number=748
Thread1: number=749
Thread1: number=751
Thread1: number=753
Thread1: number=755
Thread1: number=757
Thread1: number=759
Thread1: number=760
Thread1: number=762
Thread1: number=764
Thread1: number=766
Thread1: number=768
Thread1: number=770
Thread1: number=771
Thread1: number=773
Thread1: number=775
Thread1: number=777
Thread1: number=779
Thread1: number=781
Thread1: number=782
Thread1: number=784
Thread1: number=786
Thread1: number=788
Thread1: number=790
Thread1: number=792
Thread1: number=793
Thread1: number=795
Thread1: number=797
Thread1: number=799
Thread1: number=801
Thread1: number=803
Thread1: number=804
Thread1: number=806
Thread1: number=808
Thread1: number=810
Thread1: number=812
Thread1: number=814
Thread1: number=815
Thread1: number=817
Thread1: number=819
Thread1: number=821
Thread1: number=823
Thread1: number=825
Thread1: number=826
Thread1: number=828
Thread1: number=830
Thread1: number=832
Thread1: number=834
Thread1: number=836
Thread1: number=837
Thread1: number=839
Thread1: number=841
Thread1: number=843
Thread1: number=845
Thread1: number=847
Thread1: number=848
Thread1: number=850
Thread1: number=852
Thread1: number=854
Thread1: number=856
Thread1: number=857
Thread1: number=859
Thread1: number=861
Thread1: number=863
Thread1: number=865
Thread1: number=867
Thread1: number=868
Thread1: number=870
Thread1: number=872
Thread1: number=874
Thread1: number=876
Thread1: number=878
Thread1: number=879
Thread1: number=881
Thread1: number=883
Thread1: number=885
Thread1: number=887
Thread1: number=889
Thread1: number=890
Thread1: number=892
Thread1: number=894
Thread1: number=896
Thread1: number=898
Thread1: number=900
Thread1: number=901
Thread1: number=903
Thread1: number=905
Thread1: number=907
Thread1: number=909
Thread1: number=911
Thread1: number=912
Thread1: number=914
Thread1: number=916
Thread1: number=918
Thread1: number=920
Thread1: number=922
Thread1: number=923
Thread1: number=925
Thread1: number=927
Thread1: number=929
Thread1: number=931
Thread1: number=933
Thread1: number=935
Thread1: number=936
Thread1: number=938
Thread1: number=940
Thread1: number=942
Thread1: number=944
Thread1: number=946
Thread1: number=947
Thread1: number=949
Thread1: number=951
Thread1: number=953
Thread1: number=955
Thread1: number=957
Thread1: number=958
Thread1: number=960
Thread1: number=962
Thread1: number=964
Thread1: number=966
Thread1: number=968
Thread1: number=969
Thread1: number=971
Thread1: number=973
Thread1: number=975
Thread1: number=977
Thread1: number=979
Thread1: number=980
Thread1: number=982
Thread1: number=984
Thread1: number=986
Thread1: number=988
Thread1: number=990
Thread1: number=991
Thread1: number=993
Thread1: number=995
Thread1: number=997
Thread1: number=999
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000
Thread1: number=1000

时间: 2024-10-18 02:35:02

QNX 多线程 (线程1每隔20ms读取 number;线程2每隔10ms计算一次)的相关文章

Python36 1.joinablequeue 2.线程理论 3.多线程对比多进程 4.线程的使用方式 4.1.产生 线程的两种方式 4.2.守护线程 4.3.线程安全问题 4.3.1.互斥锁 4.3.2.死锁 4.3.3.可重入锁 4.3.4.信号量

复习1.守护进程2.互斥锁(解决数据错乱的方法)3.IPC(进程间通讯)4.生产者与消费者模型 详解:1.守护进程 一个进程可以设为另一个进程的守护进程 特点:被守护的进程结束时,守护进程也会随之结束 本质:父进程交给子进程一个任务,然而父进程 先于子进程结束了,子进程的任务也就没有必要 继续执行了 格式:开始前加 p.daemon=True 2.互斥锁(解决数据错乱的方法)方法一:互斥锁 互斥 互相排斥 锁的本质:一个标志 标志的两个状态: 1.锁定 2.未锁定 什么时候用? 当多个进程要操作

(Java多线程系列七)Java内存模型和线程的三大特性

Java内存模型和线程的三大特性 多线程有三大特性:原子性.可见性.有序性 1.Java内存模型 Java内存模型(Java Memory Model ,JMM),决定一个线程对共享变量的写入时,能对另一个线程可见.从抽象的角度来看,JMM定义了线程和主内存之间的抽象关系:线程之间的共享变量存储在主内存(main memory)中,每个线程都有一个私有的本地内存(local memory),本地内存中存储了该线程以读/写共享变量的副本.本地内存是JMM的一个抽象概念,并不真实存在. 用一张图表示

iOS多线程开发(二)---线程管理

线程管理 线程管理包括创建,配置,退出三部分.主要包括创建线程的成本,线程创建,线程属性配置,线程主体入口函数编写,线程中断等 一,线程创建成本 1,为辅助线程分配的堆栈空间大小,便于系统和进程管理,以及为函数参数和局部变量分配空间 A,内核数据结构(kernel data structures)---大约1KB,This memory is used to store the thread data structures and attributes, much of which is all

编程思想之多线程与多进程(1)——以操作系统的角度述说线程与进程

什么是线程 什么是线程?线程与进程与有什么关系?这是一个非常抽象的问题,也是一个特别广的话题,涉及到非常多的知识.我不能确保能把它讲的话,也不能确保讲的内容全部都正确.即使这样,我也希望尽可能地把他讲通俗一点,讲的明白一点,因为这是个一直困扰我很久的,扑朔迷离的知识领域,希望通过我的理解揭开它一层一层神秘的面纱. 任务调度 线程是什么?要理解这个概念,须要先了解一下操作系统的一些相关概念.大部分操作系统(如Windows.Linux)的任务调度是采用时间片轮转的抢占式调度方式,也就是说一个任务执

iOS边练边学--多线程介绍、NSThread的简单实用、线程安全以及线程之间的通信

一.iOS中的多线程 多线程的原理(之前多线程这块没好好学,之前对多线程的理解也是错误的,这里更正,好好学习这块) iOS中多线程的实现方案有以下几种 二.NSThread线程类的简单实用(直接上代码) 三.多线程的安全隐患 资源共享 1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源 比如多个线程访问同一个对象.同一个变量.同一个文件 当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题(存钱取钱的例子,多个售票员卖票的例子) 安全隐患解决的方法 --- 互斥锁(图解)

Android多线程研究(7)——Java5中的线程并发库

从这一篇开始我们将看看Java 5之后给我们添加的新的对线程操作的API,首先看看api文档: java.util.concurrent包含许多线程安全.测试良好.高性能的并发构建块,我们先看看atomic包下的AtomicInteger. import java.util.concurrent.atomic.AtomicInteger; public class AtomicIntegerTest { private static AtomicInteger data = new Atomic

JAVA之旅(十四)——静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制

JAVA之旅(十四)--静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制 JAVA之旅,一路有你,加油! 一.静态同步函数的锁是class对象 我们在上节验证了同步函数的锁是this,但是对于静态同步函数,你又知道多少呢? 我们做一个这样的小实验,我们给show方法加上static关键字去修饰 private static synchronized void show() { if (tick > 0) { try { Thread

“全栈2019”Java多线程第四章:设置和获取线程名称

难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多线程第四章:设置和获取线程名称 下一章 "全栈2019"Java多线程第五章:线程睡眠sleep()方法详解 学习小组 加入同步学习小组,共同交流与进步. 方式一:关注头条号Gorhaf,私信"Java学习小组". 方式二:关注公众号Gorhaf,回复"Jav

26_多线程_第26天(Thread、线程创建、线程池)

今日内容介绍1.多线程2.线程池 01进程概念 A:进程概念 a:进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行, 即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. 02线程的概念 A:线程的概念 a:线程:线程是进程中的一个执行单元(执行路径),负责当前进程中程序的执行, 一个进程中至少有一个线程.一个进程中是可以有多个线程的, 这个应用程序也可以称之为多线程程序. 简而言之:一个程序运行后至少有一个进程,一个进程中可以包含多个线程 03深入线程的概念 A:深