Linux 線程學習(一)

來源:互聯網
上載者:User

     學習資料來自APUE 這本著名的書籍,部分來自wikipedia,我將分步驟一步一步的將自己的學到的東西分享出來!

  首先給出的整體的內容

     

1)基本概念

   線程,又稱為輕量級線程,是程式執行流的最小單元!進程是分為使用者級線程和核心級線程!

 常見的模型有

    線程:進程

     1 :1   指的是 kernel-level thread  ,on Linux  usual c library implement this approach!

     N: 1  指的是 使用者級線程! An N:1 model implies that all application-level threads map to a single kernel-level scheduled entity; the kernel has no knowledge of the application threads. With this approach, context switching can be done very quickly and, in addition, it can be implemented even on simple kernels which do not support threading

     對於使用者級線程,其好處是能夠快速的進行context switch 轉換,因為它們之間是沒有進入到核心模式下的!缺點就是,不能夠利用硬體來加速!因為每次只能有一個thread

能夠被processor調用!

2)特點

  線程,是對進程進一步的細分,它的引入是為了更好並發執行程式,提高執行效率。線程之間共用進程資源,一個線程是可以建立或者是撤銷一個線程。

  線上程 中 context switch 速度 比在進程間的速度要快很多。線程包含必需的系統資源,比如pc,一些必須的寄存器和堆棧,線程id等!

 3)與進程的區別

    1)進程含有獨立的地址空間,而線程是沒有的,線程必須在進程中才有!線程是必須依靠線程才能夠生存 。一個進程可以看作是一個獨立的線程

    2)線程中含有少量的自己必需的狀態資訊,而進程中則包含大量的資訊!

    3)在進程中進行context switch 比線上程中context switch 慢很多!

 4) 線程的建立以及終止

       首先介紹幾個比較重要的函數

       這些函數的標頭檔都是#include<pthread.h>

       int pthread_equal(pthread_t tid1,pthread_t tid2)//比較兩個線程id是否相等

       pthread_t  pthread_self(void)//返回的是自己的線程id

       int pthread(pthread_t *restrict tidp,const pthread_attr_t *restrict attr, void *(*start_rtn)(void),void *restrict arg);//建立一個id

       給出一段代碼做參考!

       

 1 #include<pthread.h> 2 #include<stdio.h> 3 void * func(void *arg) 4 { 5   printf("the pid of the programming is %d\n",getpid()); 6   printf("the new thread is :%u\n",pthread_self()); 7   return ((void*) 0); 8 } 9 int main()10 {11    pthread_t tid=pthread_self();//tid 是main這個線程的id12    pthread_t ntid; 13    printf("the main thread is %u:\n",tid);14    pid_t pid=getpid();15    printf("the pid of the process is %d:\n",pid);16    int err=pthread_create(&ntid,NULL,func,NULL);17    if(err!=0)18      printf("create thread error\n");19    sleep(1);20    return 0;21 22 }

  從這個程式中我麼可以看到建立的線程和以前的線程是在同一個進程中的,但是它們的線程id是不同的!

 線程的終止:

  1)不能夠使用一些比較常用的函數 ,比如說_exit,exit 等函數,在任何一個線程中使用這些函數,將使得進程給終止。

  2)有三種方式來終止一個線程

       1.線程被其他的線程給取消

       2.線程調用pthread_exit() 函數

       3.線程從啟動常式中返回,返回的是線程的退出碼!

   我們通常用pthread_join()函數來取出線程的退出碼

  給出個簡單利用的例子

  

 1 #include<pthread.h> 2 #include<stdio.h> 3 void * func(void *arg) 4 { 5         printf("the thread1 exit \n"); 6         pthread_exit((void *)1); 7 } 8 void * func1(void *arg) 9 {10         printf("the thread2 exit \n");11         pthread_exit((void *)2);12 }13 int main()14 {15         int err;16         pthread_t tid1,tid2;17         void *tret;18         err=pthread_create(&tid1,NULL,func,NULL);19         if(err!=0)20                 printf("can't create thread 1:\n");21         err=pthread_create(&tid2,NULL,func1,NULL);22         if(err!=0)23                 printf("can't create thread 2:\n");24         err=pthread_join(tid1,&tret);25         if(err!=0)26                 printf("can't join thread1\n");27         printf("thread1 exit code %d\n",(int)tret);28         err=pthread_join(tid2,&tret);29         if(err!=0)30                 printf("can't join thread2\n");31         printf("thread2 exit code %d\n",(int)tret);32         return 0;33 34 35 36 37 }

注意編譯的時候 應該是 gcc -o  ... .... -lpthread

5)線程的同步將在下一篇講解!

  

  

 

相關文章

聯繫我們

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