色偷偷偷亚洲综合网另类,亚洲欧美另类在线观看,欧美午夜激情在线,久久久精品一区

      當(dāng)前位置:首頁 > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > Linux下多線程機(jī)制

      Linux下多線程機(jī)制 時間:2018-09-26      來源:未知

      1 線程不能獨立運行,要依附于進(jìn)程

      2 如果創(chuàng)建一個子線程只需要重新分配棧空間

      3 多個線程可以并行運行

      4 線程之間可以有共同的全局變量(全局區(qū),任何線程都可以訪問)

      5 多線程效率高

      如何創(chuàng)建子線程(在進(jìn)程中創(chuàng)建線程)

      #include

      int pthread_create(pthread_t *thread, pthread_arrt_t *attr, void *(*start_routine)(void *), void *arg);

      功能:創(chuàng)建一個子線程

      參數(shù):

      thread [出參],當(dāng)程序執(zhí)行此函數(shù),此函數(shù)會傳出一個值,線程的id

      attr [入?yún),通常為NULL, 線程的屬性,如果為NULL, 屬性默認(rèn)(線程優(yōu)先級,線程堆棧大小....)

      start_routine 函數(shù)指針,需要傳進(jìn)來一個函數(shù)名,然后會自動執(zhí)行此函數(shù),

      此函數(shù)就是線程需要執(zhí)行的程序

      arg 此參數(shù)專門給第三個參數(shù)start_routine使用的,此參數(shù),作為start_routine函數(shù)的參數(shù)

      int process(int (*p)(int, int), int a, int b)

      {

      p(a, b);

      }

      創(chuàng)建一個線程實例:

      #include

      #include

      void *fun(void *p)

      {

      while(1)

      {

      printf("thread 1 running\n");

      sleep(1);

      }

      }

      int main()

      {

      pthread_t id;

      pthread_create(&id, NULL, fun, NULL);

      printf("%lu\n", id);

      while(1) //目的:讓主進(jìn)程不結(jié)束

      {

      ;

      }

      }

      編譯時: gcc -o hello hello.c -lpthread //多線程是一個第三庫函數(shù),所以要加-lpthread

      多線程的好處:

      要實現(xiàn) 1 接收鍵盤輸入 2 同時每隔一秒鐘打印一下家中的溫度

      pthread_join(); ///函數(shù)功能:主進(jìn)程如果執(zhí)行到此函數(shù),將阻塞,等待子線程結(jié)束

      #include

      #include

      void *fun(void *p)

      {

      while(1)

      {

      printf("thread 1 running\n");

      sleep(1);

      }

      }

      int main()

      {

      pthread_t id;

      pthread_create(&id, NULL, fun, NULL);

      printf("%lu\n", id);

      pthread_join(id, NULL); //阻塞,等待子線程結(jié)束, 節(jié)省cpu資源

      }

      線程參數(shù)

      //////////////實例:傳遞字符串給線程/////////////////////

      #include

      #include

      void *fun(void *p)

      {

      //char *q = (char *)p;

      while(1)

      {

      printf("%s\n", (char *)p);

      sleep(1);

      }

      }

      int main()

      {

      char s[] = "hello world";

      pthread_t id;

      pthread_create(&id, NULL, fun, s);

      printf("%lu\n", id);

      pthread_join(id, NULL); //阻塞,等待子線程結(jié)束, 節(jié)省cpu資源

      }

      //////////////實例:傳遞整形變量給線程/////////////////////

      #include

      #include

      void *fun(void *p)

      {

      int *q = (int *)p;

      while(1)

      {

      printf("%d\n", *q);

      sleep(1);

      }

      }

      int main()

      {

      int a = 100;

      pthread_t id;

      pthread_create(&id, NULL, fun, &a);

      printf("%lu\n", id);

      pthread_join(id, NULL); //阻塞,等待子線程結(jié)束, 節(jié)省cpu資源

      }

      void *p ///無類型指針,可以定義變量,但不可以使用(*p = 100, p++, p--)

      ///無類型指針只能賦值給另一個帶類型的指針變量

      線程的同步與互斥

      同步(按照預(yù)想的順序執(zhí)行)

      M->Y->M->Y->M->Y

      M->YYY->M->YYY......

      互斥

      你用,我不能用(如:網(wǎng)絡(luò)打印機(jī),A 打印時, B不可以打印)

      /////互斥例子

      #include

      #include

      int a[10] = { 0 }; ///共享資源

      int i;

      void *fun1()

      {

      while(1)

      {

      int i;

      for(i = 0; i < 10; i++)

      {

      a[i] = i;

      }

      sleep(2);

      for(i = 0; i < 10; i++)

      {

      printf("a[%d] is %d\n", i, a[i]);

      }

      }

      }

      void *fun2()

      {

      while(1)

      {

      sleep(1);

      for(i = 0; i < 10; i++)

      {

      a[i] = 1;

      }

      }

      }

      int main()

      {

      pthread_t id1, id2;

      pthread_create(&id1, NULL, fun1, NULL);

      pthread_create(&id2, NULL, fun2, NULL);

      pthread_join(id1, NULL);

      }

      //////線程同步(mutex)

      A 使用共享資源,加鎖,如果這時B也使用共享資源,

      B也加鎖,但是鎖已經(jīng)被A占用,B只能等,一旦A解鎖,B運行,加鎖,使用共享資源

      //////互斥鎖,用來解決共享資源同步的問題,(互斥鎖初始化)

      pthread_mutex_t 互斥鎖類型結(jié)構(gòu)體

      1 創(chuàng)建互斥鎖(初始化互斥鎖)

      pthread_mutex_t mutex;

      int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr *mutexattr);

      mutex [出參] 創(chuàng)建互斥鎖,會將新建的互斥鎖信息傳遞給mutex變量

      mutexattr 互斥鎖屬性,默認(rèn)為NULL

      例:

      pthread_mutex_init(&mutex, NULL); //創(chuàng)建并初始化互斥鎖

      2 加鎖

      一旦某個線程使用共享資源,就加鎖

      int pthread_mutex_lock(pthread_mutex_t *mutex); //如果加鎖不成功(某個線程已經(jīng)加鎖),阻塞

      3 解鎖

      int pthread_mutex_unlock(pthread_mutex_t *mutex); //如果某個線程正在等待加鎖,那么這線程進(jìn)入就緒態(tài)

      #include

      #include

      int a[10] = { 0 }; ///共享資源

      int i;

      pthread_mutex_t mutex;

      void *fun1()

      {

      while(1)

      {

      int i;

      pthread_mutex_lock(&mutex);

      for(i = 0; i < 10; i++)

      {

      a[i] = i;

      }

      sleep(2);

      for(i = 0; i < 10; i++)

      {

      printf("a[%d] is %d\n", i, a[i]);

      }

      pthread_mutex_unlock(&mutex);

      sleep(1);

      }

      }

      void *fun2()

      {

      while(1)

      {

      sleep(1);

      pthread_mutex_lock(&mutex);

      for(i = 0; i < 10; i++)

      {

      a[i] = 1;

      }

      pthread_mutex_unlock(&mutex);

      }

      }

      int main()

      {

      pthread_t id1, id2;

      pthread_mutex_init(&mutex, NULL);

      pthread_create(&id1, NULL, fun1, NULL);

      pthread_create(&id2, NULL, fun2, NULL);

      pthread_join(id1, NULL);

      }

      ////////信號量 (semaphore 簡寫 sem)

      同樣可以解決共享資源互斥和同步的問題

      信號量可以控制多個共享資源被訪問互斥的問題

      #include

      sem_t sem;

      1 創(chuàng)建信號量(初始化信號量)

      int sem_init(sem_t *sem, int pshared, int value);

      sem [出參], 在創(chuàng)建信號量時,傳出的信號量結(jié)構(gòu)體

      pshared 通常寫0,代表此信號量在多線程之間使用

      value 共享資源個數(shù)

      sem_init(&sem, 0, 3);

      sem_init(&sem, 0, 1);

      sem_init(&sem, 0, 0);

      2 請求信號量

      sem_wait(&sem); //如果共享資源個數(shù)不為0, 請求成功,使用共享資源,然后共享資源個數(shù)-1

      //當(dāng)共享資源個數(shù)為0時,請求失敗,阻塞

      3 釋放信號量

      sem_post(&sem); //釋放信號量,如果有線程正在阻塞等待信號量,那么阻塞解除,

      //如果沒有線程正在等待信號量,共享資源個數(shù)+1

      四個線程共享信號量實例

      sem_init(&sem, 0, 3); //表示共享資源個數(shù)有三個

      線程A 調(diào)用sem_wait(&sem); //共享資源個數(shù)-1, 2

      線程B 調(diào)用sem_wait(&sem); //共享資源個數(shù)-1, 1

      線程C 調(diào)用sem_wait(&sem); //共享資源個數(shù)-1, 0

      線程D 調(diào)用sem_wait(&sem); //共享資源個數(shù)已經(jīng)為0, 線程D阻塞

      如果線程A 調(diào)用sem_post(&sem); //線程D 解除阻塞

      線程屬性

      線程可以設(shè)置堆棧大小,可以設(shè)置線程優(yōu)先級

      默認(rèn)情況堆棧大小(有些系統(tǒng)1M, 有些2M, 有些4M, 有些8M)

      如果定義一個局部變量占用空間特別大,要改堆棧大小

      測試線程堆棧大小

      #include

      #include

      void *fun()

      {

      int a[1000000]; //約等于 4M 1M 1024k 1k 1024B

      while(1)

      {

      printf("thread 1\n");

      sleep(1);

      }

      }

      int main()

      {

      pthread_t id1;

      pthread_create(&id1, NULL, fun, NULL);

      pthread_join(id1, NULL);

      }

      pthread_create(pthread_t *thread, pthread_attr_t *attr, void*(*start_routine)(void *), void *arg);

      第二個參數(shù) pthread_attr_t *attr; 線程屬性,默認(rèn)NULL

      1 獲得線程默認(rèn)屬性,賦值給attr

      pthread_attr_t attr;

      pthread_attr_init(&attr); // 功能:獲取線程的默認(rèn)屬性,會更改attr的值,給一個默認(rèn)值

      2 設(shè)置線程的堆棧大小屬性

      pthread_attr_setstacksize(&attr, 14000000); ///功能:設(shè)置堆棧大小屬性

      #include

      #include

      void *fun()

      {

      int a[1000000]; //約等于 4M 1M 1024k 1k 1024B

      while(1)

      {

      printf("thread 1\n");

      sleep(1);

      }

      }

      int main()

      {

      pthread_t id1;

      pthread_attr_t attr;

      pthread_attr_init(&attr); // 功能:獲取線程的默認(rèn)屬性,會更改attr的值,給一個默認(rèn)值

      pthread_attr_setstacksize(&attr, 14000000); ///功能:設(shè)置堆棧大小屬性

      pthread_create(&id1, &attr, fun, NULL);

      pthread_join(id1, NULL);

      }

      上一篇:Exynos 4412 看門狗定時器中斷

      下一篇:Android異步加載AsyncTask的使用

      熱點文章推薦
      華清學(xué)員就業(yè)榜單
      高薪學(xué)員經(jīng)驗分享
      熱點新聞推薦
      前臺專線:010-82525158 企業(yè)培訓(xùn)洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠(yuǎn)見科技集團(tuán)有限公司 版權(quán)所有 ,京ICP備16055225號-5京公海網(wǎng)安備11010802025203號

      回到頂部

      色偷偷偷亚洲综合网另类,亚洲欧美另类在线观看,欧美午夜激情在线,久久久精品一区
      主站蜘蛛池模板: www.欧美视频| 最近的2019中文字幕免费一页| 亚洲色图五月天| 欧美日韩在线视频一区| 国产精品久久久久久久久免费 | 国产欧美一区二区三区视频 | 超碰日本道色综合久久综合| 欧美性一区二区三区| 日韩精品久久久久| 久久久久久综合网天天| 亚洲国产又黄又爽女人高潮的| 欧美精品性视频| 国产一区二区丝袜| 欧美成人免费小视频| 国产欧美在线看| 欧美成aaa人片在线观看蜜臀| 国产精品美女免费看| 久久精品视频在线播放| 国产剧情久久久久久| 久久综合伊人77777| 91精品中文在线| 欧美激情精品久久久久久蜜臀| 亚洲精品美女久久| 久久青草精品视频免费观看| 亚洲天堂开心观看| 国产精品爱久久久久久久| 久久精品这里热有精品| 91精品视频播放| 国模视频一区二区三区| 在线观看精品国产视频| 国产日韩精品一区二区| 欧美日本国产在线| 国产视频一区在线| 国产精品久在线观看| 日韩欧美亚洲成人| 中文欧美在线视频| 亚洲一区久久久| 国产成人鲁鲁免费视频a| 精品久久久视频| 最近2019免费中文字幕视频三| 亚洲第一网中文字幕|