linux 線程學習(一)

來源:互聯網
上載者:User

線程與進程的區別

-----------------------------------------------------

1.節儉:在Linux系統下,啟動新的進程,必須分配給它獨立的地址空間,建立眾多的資料表來維護它的程式碼片段、堆棧段和資料區段。運行於一個進程中的多個線程,它們彼此之間使用相同的地址空間,共用大部分資料,啟動一個線程所花費的空間遠遠小於啟動一個進程所花費的空間,而且,線程間彼此切換所需的時間也遠遠小於進程間切換所需要的時間。

2.通訊方便:不同進程,它們具有獨立的資料空間,要進行資料的傳遞只能通過通訊的方式進行,這種方式不僅費時,而且很不方便。線程則不然,由於同一進程下的線程之間共用資料空間,所以一個線程的資料可以直接為其它線程所用,這不僅快捷,而且方便。

 

本身的優點

-----------------------------------------------------

1) 提高應用程式響應。這對圖形介面的程式尤其有意義,當一個操作耗時很長時,整個系統都會等待這個操作,此時程式不會響應鍵盤、滑鼠、菜單的操作,而使用多線程技術,將耗時間長度的操作(time consuming)置於一個新的線程,可以避免這種尷尬的情況。
2) 使多CPU系統更加有效。作業系統會保證當線程數不大於CPU數目時,不同的線程運行於不同的CPU上。
3) 改善程式結構。一個既長又複雜的進程可以考慮分為多個線程,成為幾個獨立或半獨立的運行部分,這樣的程式會利於理解和修改。

 

線程編程

----------------------------------------------------

Linux系統下的多線程遵循POSIX線程介面,稱為pthread。編寫Linux下的多線程程式,需要使用標頭檔pthread.h,串連時需要使用庫libpthread.a。

 

#include <pthread.h>
#include <stdio.h>

void thread(void)
{
  int i;
  for(i=0;i<3;i++)
  printf("This is a pthread./n");
}
  
int main(void)
{
  pthread_t id;
  int i,ret;
  ret=pthread_create(&id,NULL,(void *) thread,NULL);
  if(ret!=0){
  printf ("Create pthread error!/n");
  exit (1);
  }
  for(i=0;i<3;i++)
        printf("This is the main process./n");
  pthread_join(id,NULL);
  return (0);
}

 

gcc  -o thread -lpthread test //pthread庫不是Linux系統預設的庫,串連時需要使用庫libpthread.a,

運行thread ,我們得到如下結果:
  This is the main process.
  This is a pthread.
  This is the main process.
  This is the main process.
  This is a pthread.
  This is a pthread.
再次運行thread ,我們可能得到如下結果:
  This is a pthread.
  This is the main process.
  This is a pthread.
  This is the main process.
  This is a pthread.
  This is the main process.

 

前後兩次結果不一樣,這是兩個線程爭奪CPU資源的結果。

 

線程函數介紹

----------------------------------------------------------------

int pthread_create( pthread_t *restrict tidp, const pthread_attr_t *restrict attr,
                                void *(*start_rtn)(void),  void *restrict arg );

 

第一個參數為指向線程標識符的指標。
第二個參數用來設定線程屬性。
第三個參數是線程運行函數的起始地址。
最後一個參數是運行函數的參數。

 

註:如果想傳遞參數給線程函數,可以通過其參數arg,其類型是void *。如果你需要傳遞多個參數的話,可以考慮將這些參數組成一個結構體來傳遞。另外,由於類型是void *,所以你的參數不可以被提前釋放掉。

 

int pthread_join(pthread_t thread, void **value_ptr);

線程阻塞函數,將當前線程掛起,等待指定的線程結束,並回收資源。

 

 

void pthread_exit(void *rval_ptr)

單個線程退出方式有3種:從啟動常式中返回,傳回值是線程的退出碼;被其它線程取消;線程調用pthread_exit。

rval_ptr 是一個無類型指標,相當於線程的退出碼。它通過pthread_join()訪問到

 

void *   thr_fn1(void *arg)
{
    printf("thread 1 returning/n");
    return((void *)1);
}

void *   thr_fn2(void *arg)
{
    printf("thread 2 exiting/n");
    pthread_exit((void *)2);
}

pthread_create(&tid1, NULL, thr_fn1, NULL);
pthread_create(&tid2, NULL, thr_fn2, NULL);
pthread_join(tid1, &tret);//  tret = 1
pthread_join(tid2, &tret);//  tret = 2

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.