linux下多執行緒程式設計

2022-01-12 00:35:10 字數 3773 閱讀 2565

先看執行的結果:

1

主函式正在建立執行緒,...

2執行緒1被建立

3 thread1 : i'

m thread 1th

4執行緒2被建立

5 thread2 : i'

m thread 2nd

6 thread2 : number = 0

7執行緒3被建立

8主函式正在等待執行緒結束...

9 thread1 : number = 0

10 thread3 : i'

m thread 3nd

11 thread3 : number = 2

12 thread3 : number = 3

13 thread2 : number = 4

14 thread1 : number = 5

15 thread3 : number = 6

16 thread2 : number = 7

17 thread3 : number = 8

18 thread1 : number = 9

19 thread3 : number = 10

20 thread2 : number = 11

21 thread3 : number = 12

22 thread2 : number = 13

23 thread1 : number = 14

24 thread3 : number = 15

25 thread3 : number = 16

26 thread2 : number = 17

27 thread1 : number = 18

28 thread3 : number = 19

29 thread2 : number = 20

30 thread3 : number = 21

31 thread1 : number = 22

32 thread3 : number = 23

33 thread2 : number = 24

34 thread3 : number = 25

35 thread2 : number = 26

36 thread1 : number = 27

37 thread3 : number = 28

38 thread3 : number = 29

39 thread2 : number = 30

40 thread1 : number = 31

41thread3 :main函式在等我完成任務嗎?

42thread2 :main函式在等我完成任務嗎?

43thread1 :main函式在等我完成任務嗎?

44執行緒1已經結束

45執行緒2已經結束

46 執行緒3已經結束

1

//filename: threadsample.c

2 #include 3 #include 4 #include 5 #include

6 #include //

新增的標頭檔案

7#define max 30

89 pthread_t thread[3]; //

3個執行緒

10pthread_mutex_t mut;

11int number=0;12

inti;

1314

void *thread1()

2425 printf("

thread1 :main函式在等我完成任務嗎?\n");

26pthread_exit(null);27}

2829

void *thread2()

4041 printf("

thread2 :main函式在等我完成任務嗎?\n");

42pthread_exit(null);43}

4445

void *thread3()

5657 printf("

thread3 :main函式在等我完成任務嗎?\n");

58pthread_exit(null);59}

6061

void thread_create(void)

7980

void thread_wait(void)86

if(thread[1] !=0)90

if(thread[2] !=0)94

}9596int

main()

97

再次執行結果為:

1

主函式正在建立執行緒,...

2執行緒1被建立

3 thread1 : i'

m thread 1th

4 thread1 : number = 0

5 thread2 : i'

m thread 2nd

6 thread2 : number = 1

7執行緒2被建立

8執行緒3被建立

9主函式正在等待執行緒結束...

10 thread3 : i'

m thread 3nd

11 thread3 : number = 2

12 thread3 : number = 3

13 thread2 : number = 4

14 thread1 : number = 5

15 thread3 : number = 6

16 thread2 : number = 7

17 thread3 : number = 8

18 thread1 : number = 9

19 thread3 : number = 10

20 thread2 : number = 11

21 thread3 : number = 12

22 thread1 : number = 13

23 thread2 : number = 14

24 thread3 : number = 15

25 thread3 : number = 16

26 thread2 : number = 17

27 thread1 : number = 18

28 thread3 : number = 19

29 thread2 : number = 20

30 thread3 : number = 21

31 thread1 : number = 22

32 thread3 : number = 23

33 thread2 : number = 24

34 thread3 : number = 25

35 thread1 : number = 26

36 thread2 : number = 27

37 thread3 : number = 28

38 thread3 : number = 29

39 thread2 : number = 30

40 thread1 : number = 31

41thread3 :main函式在等我完成任務嗎?

42thread2 :main函式在等我完成任務嗎?

43thread1 :main函式在等我完成任務嗎?

44執行緒1已經結束

45執行緒2已經結束

46 執行緒3已經結束

編譯方法:gcc threadsample.c -o threadsample -lpthread

**非常簡單,請自行理解吧

linux下多多執行緒程式設計

thread example.c c multiple thread programming in linux author falcon e mail tunzhj03 st.lzu.edu.cn include include include include define max 10 pthr...

Linux下的多執行緒程式設計

執行緒是作業系統能夠進行排程運算的最小單位,它被包含在程序之中,是程序中的實際運作單位。一條執行緒指的是程序中乙個單一順序的控制流,乙個程序可以併發多個執行緒,每條執行緒執行不同的任務。include intpthread create pthread t pthread,const pthread...

Linux下的多執行緒程式設計

如何一次建立多個執行緒 現在對上述的互斥量的實驗程式進行分析 首先在main 函式中建立了兩個執行緒,先執行執行緒1,再執行執行緒2.執行緒1對count加互斥所,此時執行緒2就不能訪問count。只有當沉睡時間過後執行緒1對互斥量解鎖之後,執行緒2才能加鎖修改count的值並訪問,最後解鎖。try...