LINUX線程同步初探

來源:互聯網
上載者:User

標籤:logs   each   線程同步   合作   pid   size   代碼   span   寫入   

0x00.什麼是線程同步
    同步,又稱直接制約關係,是指多個線程(或進程)為了合作完成任務,必須嚴格按照規定的 某種先後次序來運行


0x01.案例代碼

 1 void* PthreadFunc(void* argc); 2 int flag_num = 1; 3  4 int main(int argc, char* argv[]) 5 { 6     pthread_t pid; 7     void* ret_val; 8  9     int create_status = pthread_create(&pid, NULL,  &PthreadFunc, NULL);10     if(0 != create_status)11     {      perror("main()->pthread_create error");12         exit(1);13     }14 15     //input data to public source area16     for(int i = 0;  i < 10; ++i)17     {18         if(flag_num == 1)19         {20             printf("1");21             flag_num = 2;22         }else{23             sleep(1);24         }25     }26 27     int join_status = pthread_join(pid, &ret_val);28     if(0 != join_status)29     {30         perror("main()->pthread_join error");31         exit(1);32     }33 34     //print array‘s data35     for(int i = 0; i < 10; ++i)36     {37         printf("%d  ", arr[i]);38     }39 40     return 0;41 }42 43 void* PthreadFunc(void* argc)44 {45         if(flag_num == 2)46         {47             printf("2");48             flag_num = 1;49         }else{50             sleep(1);51         }52 }

 

執行結果:
  [email protected]:~/projects/proj$ ./syn
  1212121212

結論:利用條件避免無休止搶佔公用資源

 

0x02.修改代碼擷取新知識點 …(⊙_⊙;)

 

 1 void* PthreadFunc(void* argc); 2 int arr[10]; 3  4 int main(int argc, char* argv[]) 5 { 6     pthread_t pid; 7     void* ret_val; 8  9     int create_status = pthread_create(&pid, NULL,  &PthreadFunc, NULL);10     if(0 != create_status)11     {      perror("main()->pthread_create error");12         exit(1);13     }14 15     //input data to public source area16     for(int i = 0;  i < 10; ++i)17     {18         arr[i] = i;19     }20 21     int join_status = pthread_join(pid, &ret_val);22     if(0 != join_status)23     {24         perror("main()->pthread_join error");25         exit(1);26     }27 28     //print array‘s data29     for(int i = 0; i < 10; ++i)30     {31         printf("%d  ", arr[i]);32     }33 34     return 0;35 }36 37 void* PthreadFunc(void* argc)38 {39     for(int i = 0;  i < 10; ++i)40     {41         arr[i] = i + 10;42     }43 }

 

執行代碼結果:
[email protected]:~/projects/proj$ ./syn
10 11 12 13 14 15 16 17 18 19

  當時出現結果一臉懵,後來查資料發現了線程是有優先順序的:main(for()) 屬於主線程,PthreadFunc(for())屬於子線程、

  主線程的優先順序大於子線程,所以main.for()先對arr進行了資料寫入,pthread.for()後寫入資料,輸出結果為線程輸入資料

 

LINUX線程同步初探

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.