In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Linux function prototype example analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "Linux function prototype example analysis"!
introduction
A conditional variable is a mechanism for synchronization using global variables shared between threads. It mainly includes two actions: one thread waits for the condition of the conditional variable to hold (at this time, it no longer occupies the CPU); the other thread makes the condition hold (gives the condition hold signal). To prevent race, the use of conditional variables is always combined with a mutex.
function prototype
1. Define conditional variables
#include /* define two conditional variables */pthread_cond_t cond_pro, cond_con;
2. Initialize and destroy conditional variables
#include int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);int pthread_cond_destroy(pthread_cond_t *cond); /* initialize conditional variables */pthread_cond_init(&cond_pro, null);pthread_cond_init(& cond_con, null);/* destroy conditional variables */pthread_cond_destroy(&cond_pro);pthread_cond_destroy(& cond_pro);
3. waiting and firing conditions
#include int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);int pthread_cond_broadcast(pthread_cond_t * cond);int pthread_cond_signal(pthread_cond_t *cond);/* wait condition */* Note: pthread_cond_wait is a blocking function. Unlock the lock and wait. When the conditions are met, you need to grab the lock before you can wake up */ pthread_cond_wait (&cond_pro,&mutex); /* trigger condition */* all threads blocked because the condition is not met in a queue in the conditional variable cond_pro *//* broadcast all blocked threads */pthread_cond_broadcast (&cond_pro);/* Signal only the first thread */pthread_cond_signal(&cond_pro);
code
/** *********************************************************************** > file name: my_con.c > author: krischou > mail:zhoujx0219@163.com > created time: tue 26 aug 2014 10:24:29 am cst ************************************************************************ /* Define mutex lock */pthread_cond_t cond_pro, cond_con; /* Define two conditional variables */* Producer thread */void* pro_handler(void *arg){ pthread_detach(pthread_self()); /* Thread resources are reclaimed by the system, not by the main thread, in which case the main thread is a server and never exits */ while(1) { pthread_mutex_lock(&mutex); while(i >= cell) { pthread_cond_wait(&cond_pro,&mutex); /* continue is polling, here blocking */ /* Release the lock and wait. The first parameter is a pointer to the structure, in which there are members to store the blocked function */ /* Not CPU*/ /* Wait until the condition is not met, need someone to tell it to wake it up *//* When it returns, the lock will come back */ } i++; if(i == 1) { /* From empty to non-empty, wake up consumers */ pthread_cond_signal(&cond_con); /* Consumer threads that are blocked will not be signaled immediately because they have to wait for the lock to be retrieved */ } printf("add i: %d \n", i); pthread_mutex_unlock(&mutex); sleep(rand() % 5 + 1); }}/* consumer thread */void* con_handler(void *arg){ pthread_detach(pthread_self()); while(1) { pthread_mutex_lock(&mutex); while(i 0) { pthread_create(arr + index, null, pro_handler, null); index++; pro_cnt--; } while(con_cnt > 0) { pthread_create(arr + index, null, con_handler, null); index++; con_cnt--; } while(1); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond_pro); pthread_cond_destroy(&cond_con); return 0;}
note
Either in the producer thread or in the consumer thread. The condition marked in yellow must be while. For example, when i>=cell, i.e. i is full, pthread_cond_wait(&cond_cno,&mutex) is executed; the producer thread is suspended. Must wait until consumer thread pthread_cond_signal(&cond_pro); wakes it up. But it is not enough for the consumer to signal it, the producer thread that has been suspended must regain the lock before it can be activated. However, since the producer cannot grab the lock immediately at the same time as the consumer signal, the value of i may change to greater than or equal to 10 at this time. So you have to use while. This may result in i>10.
At this point, I believe everyone has a deeper understanding of "Linux function prototype example analysis," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.