linux c 多執行緒執行順序解析

2021-06-20 19:40:58 字數 3408 閱讀 1119

1:多執行緒設計通常是比較麻煩的,因為它牽涉到,執行緒間的同步、和執行順序問題。在使用者沒有設定執行緒間的排程策略時,系統預設採取基於時間片輪轉的排程策略。本實驗是在預設的排程策略下,測試執行緒間的執行順序問題。

本實驗用的系統為ubuntu10.04

,thread model: posix

gcc version 4.4.3 (ubuntu 4.4.3-4ubuntu5)  2 

執行**

view plain

print?

#include

#include

#include

#include

#include

static void thread_one(char* msg); 

static void thread_two(char* msg); 

int 

main(int argc, char** argv) 

sleep(1);// @1 

printf("thread_two starting/n"); 

if(pthread_create(&th_two,null,(void*)&thread_two,msg)!=0) 

printf("main thread will sleep 1 s/n"); 

sleep(1);//@2 

return 0; 

} static void thread_one(char* msg)  } 

static void thread_two(char* msg)  } 

2.1 

主線程和子執行緒的執行順序問題

2.1.1 

主線程沒有執行等待

在主線程沒有執行等待的情況下,即@1和@2

在注釋掉的情況下。程式的執行如下。

thread_one starting

thread_two starting

main thread will sleep 1 s

由此可見,多執行緒中子執行緒的執行是在主線程有空閒的條件下。即,如果主線程忙,或者是沒有執行等待那麼,子執行緒是不會執行的。

2.1.2 

程式在@1

中執行等待,

@2中不等待

執行結果如下:

thread_one starting

i am one. loop 0

i am one. loop 1

i am one. loop 2

i am one. loop 3

i am one. loop 4

i am one. loop 5

thread_two starting

main thread will sleep 1 s

程式在執行到i am one. loop 5

時,停頓一下,再執行。

可見,主線程在@1

處,停了

1秒,等待執行緒11

執行完畢後,有時還有時間,主線程繼續等待完

1秒後,重新執行。

2.1.3 

程式在@1

中不執行等待,

@2中等待

執行結果如下:

thread_one starting

thread_two starting

main thread will sleep 1 s

i am one. loop 0

i am one. loop 1

i am one. loop 2

i am one. loop 3

i am one. loop 4

i am one. loop 5

i am two. loop 0

i am two. loop 1

i am two. loop 2

i am two. loop 3

i am two. loop 4

i am two. loop 5

可見,在兩個子執行緒執行後,主線程等待了1

秒,這是有充分的時間讓子執行緒完成執行。兩個現成的執行順序為先後順序。

2.1.4 

程式在@1

中執行等待,

@2中等待

執行結果如下:

thread_one starting

i am one. loop 0

i am one. loop 1

i am one. loop 2

i am one. loop 3

i am one. loop 4

i am one. loop 5

thread_two starting

main thread will sleep 1 s

i am two. loop 0

i am two. loop 1

i am two. loop 2

i am two. loop 3

i am two. loop 4

i am two. loop 5

程式在執行到i am one. loop 5

時,停頓了一下。等待夠

1秒後繼續執行。

在主線中,等待充分的時間使子執行緒執行,可以保證順序執行。

2.1.5 @1,@2,

@3,@4中個等待1秒。

程式執行結果如下:

thread_one starting

i am one. loop 0

thread_two starting

main thread will sleep 1 s

i am one. loop 1

i am two. loop 0

在@1處,主線程等待了

1s,讓子執行緒

1執行。子執行緒

1執行一次迴圈,等待了

1s。由於超過了主線程的等待時間,主線程在等待夠

1s後,繼續執行。由於子執行緒

1在子執行緒

2顯示輸出時,被啟用,所以子執行緒又迴圈一次後,子執行緒

2輸出結果。

2.1.6 @1,@3,

@4中個等待1秒,

@2中等待3秒

程式執行如下:

thread_one starting

i am one. loop 0

thread_two starting

main thread will sleep 3 s

i am one. loop 1

i am two. loop 0

i am one. loop 2

i am two. loop 1

i am one. loop 3

i am two. loop 2

在主線程等待3

秒時,由於執行緒

1和執行緒

2都已經執行。並且執行一次迴圈後,等待

1秒。所以,執行的結果,時執行緒

1和執行緒

2交替執行。

java多執行緒執行順序

我們建立兩個執行緒,讓奇數執行緒輸出奇數,偶數執行緒執行輸出偶數。先來看看 實現 package test import org.ietf.jgss.oid public class threadnum extends thread public void run system.out.printl...

多執行緒按照順序執行

自己瀏覽了多個的文章,自己的理解加上的思路,彙總成了這篇文章 1.如何讓多執行緒按照既定順序執行 2.原理 1.直接上 final long start system.currenttimemillis thread t1 new thread catch interruptedexception ...

控制多執行緒執行順序

雖然專案用不上,先備份吧,控制多執行緒執行順序有兩種方法 1.通過join方法保證多執行緒的順序性的特性 join 讓主線程等待子執行緒結束後才能繼續執行 public static void main string args throws interrupterexception 2.excuto...