最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

linux下的C\C++多進(jìn)程多線程編程實(shí)例詳解

 更新時(shí)間:2017年04月24日 16:50:33   投稿:lqh  
這篇文章主要介紹了linux下的C\C++多進(jìn)程多線程編程實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

linux下的C\C++多進(jìn)程多線程編程實(shí)例詳解

1、多進(jìn)程編程

#include <stdlib.h> 
#include <sys/types.h> 
#include <unistd.h> 
 
int main() 
{ 
  pid_t child_pid; 
 
  /* 創(chuàng)建一個(gè)子進(jìn)程 */ 
  child_pid = fork(); 
  if(child_pid == 0) 
  { 
    printf("child pid\n"); 
    exit(0); 
  } 
  else 
  { 
    printf("father pid\n"); 
    sleep(60); 
  } 
   
  return 0; 
} 

 2、多線程編程

#include <stdio.h> 
#include <pthread.h> 
 
struct char_print_params 
{ 
  char character; 
  int count; 
}; 
 
void *char_print(void *parameters) 
{ 
  struct char_print_params *p = (struct char_print_params *)parameters; 
  int i; 
 
  for(i = 0; i < p->count; i++) 
  { 
    fputc(p->character,stderr); 
  } 
 
  return NULL; 
} 
 
int main() 
{ 
  pthread_t thread1_id; 
  pthread_t thread2_id; 
  struct char_print_params thread1_args; 
  struct char_print_params thread2_args; 
 
  thread1_args.character = 'x'; 
  thread1_args.count = 3000; 
  pthread_create(&thread1_id, NULL, &char_print, &thread1_args); 
 
  thread2_args.character = 'o'; 
  thread2_args.count = 2000; 
  pthread_create(&thread2_id, NULL, &char_print, &thread2_args); 
 
  pthread_join(thread1_id, NULL); 
  pthread_join(thread2_id, NULL); 
 
  return 0; 
} 

 3、線程同步與互斥

1)、互斥

pthread_mutex_t mutex; 
pthread_mutex_init(&mutex, NULL); 
 
/*也可以用下面的方式初始化*/ 
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 
 
pthread_mutex_lock(&mutex); 
/* 互斥  */ 
 
thread_flag = value; 
 
pthread_mutex_unlock(&mutex); 

2)、條件變量

int thread_flag = 0; 
pthread_mutex_t mutex; 
pthread_cond_t thread_flag_cv;\ 
 
void init_flag() 
{ 
  pthread_mutex_init(&mutex, NULL); 
  pthread_cond_init(&thread_flag_cv, NULL); 
  thread_flag = 0; 
} 
 
void *thread_function(void *thread_flag) 
{ 
  while(1) 
  { 
    pthread_mutex_lock(&mutex); 
    while(thread_flag != 0 ) 
    { 
      pthread_cond_wait(&thread_flag_cv, &mutex); 
    } 
    pthread_mutex_unlock(&mutex); 
 
    do_work(); 
  } 
 
  return NULL; 
} 
 
void set_thread_flag(int flag_value) 
{ 
  pthread_mutex_lock(&mutex); 
  thread_flag = flag_value; 
 
  pthread_cond_signal(&thread_flag_cv); 
  pthread_mutex_unlock(&mutex); 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • Linux使用fdisk實(shí)現(xiàn)磁盤分區(qū)過(guò)程圖解

    Linux使用fdisk實(shí)現(xiàn)磁盤分區(qū)過(guò)程圖解

    這篇文章主要介紹了Linux使用fdisk實(shí)現(xiàn)磁盤分區(qū)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Linux中的幾種最佳文件系統(tǒng)

    Linux中的幾種最佳文件系統(tǒng)

    這篇文章主要介紹了Linux中的幾種最佳文件系統(tǒng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Linux靜態(tài)鏈接庫(kù)使用類模板的快速排序算法

    Linux靜態(tài)鏈接庫(kù)使用類模板的快速排序算法

    這篇文章主要介紹了Linux下編譯使用靜態(tài)鏈接庫(kù)-當(dāng)靜態(tài)鏈接庫(kù)遇到模板類的快速算法問(wèn)題。
    2017-11-11
  • CentOS查看壓縮包文件列表實(shí)現(xiàn)方式

    CentOS查看壓縮包文件列表實(shí)現(xiàn)方式

    這篇文章主要介紹了CentOS查看壓縮包文件列表實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2026-03-03
  • xampp apache啟動(dòng)失效問(wèn)題的解決方法

    xampp apache啟動(dòng)失效問(wèn)題的解決方法

    在windows上使用xampp搭建php的開發(fā)環(huán)境,后來(lái)又安裝了oracle 10g。
    2009-10-10
  • 探討如何減少Linux服務(wù)器TIME_WAIT過(guò)多的問(wèn)題

    探討如何減少Linux服務(wù)器TIME_WAIT過(guò)多的問(wèn)題

    本篇文章是對(duì)如何減少Linux服務(wù)器TIME_WAIT過(guò)多的問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Linux中的冷熱頁(yè)機(jī)制簡(jiǎn)述

    Linux中的冷熱頁(yè)機(jī)制簡(jiǎn)述

    這篇文章主要為大家詳細(xì)介紹了Linux中的冷熱頁(yè)機(jī)制,什么是冷熱頁(yè)?為什么要有冷熱頁(yè)?感興趣的小伙伴們可以參考一下
    2016-09-09
  • Linux shell中的test命令用法教程

    Linux shell中的test命令用法教程

    這篇文章主要給大家介紹了Linux shell中的test命令用法的相關(guān)資料,test命令是shell環(huán)境中測(cè)試條件表達(dá)式的實(shí)用工具,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-04-04
  • Ubuntu修改IP的三種方法

    Ubuntu修改IP的三種方法

    在 Ubuntu 系統(tǒng)中配置或修改靜態(tài) IP 地址,主要依賴于系統(tǒng)的版本以及是否使用圖形界面,本文給大家介紹了Ubuntu修改IP的三種常用方法,并通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下
    2025-06-06
  • linux less命令實(shí)例詳解

    linux less命令實(shí)例詳解

    在本篇文章了小編給大家整理了關(guān)于linux less命令詳解的相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-02-02

最新評(píng)論

仪征市| 名山县| 措美县| 高雄县| 平乐县| 兰州市| 肥西县| 上虞市| 大关县| 油尖旺区| 垫江县| 沭阳县| 双辽市| 洪湖市| 会理县| 彰化县| 韶关市| 砚山县| 永兴县| 梅河口市| 怀宁县| 区。| 且末县| 贵定县| 福建省| 南丰县| 彰化市| 道孚县| 东丰县| 乐至县| 蓬莱市| 铜梁县| 共和县| 木兰县| 莒南县| 巨鹿县| 宜春市| 县级市| 南漳县| 开平市| 荆州市|