#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