In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the linux C++ multi-process and multi-thread case analysis related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this linux C++ multi-process and multi-thread case analysis article will have a harvest, let's take a look.
1. Multi-process programming
# include # include # include int main () {pid_t child_pid; / * create a child process * / child_pid = fork (); if (child_pid = = 0) {printf ("child pid\ n"); exit (0);} else {printf ("father pid\ n"); sleep (60);} return 0;}
2. Multithread programming
# 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; iTunes +) {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. Thread synchronization and mutual exclusion
1), mutually exclusive
Pthread_mutex_t mutex; pthread_mutex_init (& mutex, null); / * you can also initialize * / pthread_mutex_t mutex = pthread_mutex_initializer; pthread_mutex_lock (& mutex); / * mutually exclusive * / thread_flag = value; pthread_mutex_unlock (& mutex) as follows
2), condition variable
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);} this is the end of the article on "Multi-process and Multi-thread instance Analysis of C++ under linux". Thank you for reading! I believe you all have a certain understanding of the knowledge of "C++ multi-process and multi-thread case analysis under linux". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.