QNX Multithreading (thread 1 reads number every 20ms, thread 2 computes every 10ms)

Source: Internet
Author: User

#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;
}

The results show:

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

QNX Multithreading (thread 1 reads number every 20ms, thread 2 computes every 10ms)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.