最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C++多進程和多線程編程的方法是什么

這篇文章主要介紹了C++多進程和多線程編程的方法是什么的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇C++多進程和多線程編程的方法是什么文章都會有所收獲,下面我們一起來看看吧。

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:申請域名、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、路南網(wǎng)站維護、網(wǎng)站推廣。

1、多進程編程

#include  
#include  
#include  
 
int main() 
{ 
  pid_t child_pid; 
 
  /* 創(chuàng)建一個子進程 */ 
  child_pid = fork(); 
  if(child_pid == 0) 
  { 
    printf("child pid\n"); 
    exit(0); 
  } 
  else 
  { 
    printf("father pid\n"); 
    sleep(60); 
  } 
   
  return 0; 
}

 2、多線程編程

#include  
#include  
 
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); 
}

關(guān)于“C++多進程和多線程編程的方法是什么”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“C++多進程和多線程編程的方法是什么”知識都有一定的了解,大家如果還想學(xué)習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


分享文章:C++多進程和多線程編程的方法是什么
網(wǎng)站URL:http://fisionsoft.com.cn/article/gdeied.html