Use libuv to implement producer and consumer modes.
Producer and Consumer model (Consumer + Producer model)
Used to perform time-consuming operations (Production thread), Allocated to one or more additional threads for execution (Consumption thread) To improveProduction threadResponse Speed (concurrency)
Definition
1 typedef std: unique_ptr <Work> WorkPtr; 2 std: queue <WorkPtr> work_queue; 3 uv_mutex_t mutex; 4 uv_cond_t cond;
Production
1 // adding new work2 WorkPtr newWork (new Work); 3 uv_mutex_lock (& mutex); 4 work_queue.push (std: move (new_work); 5 uv_cond_signal (& cond ); 6 uv_mutex_unlock (& mutex );
Consumption
1 while (true) 2 {3 // copying work that has to be done 4 uv_mutex_lock (& mutex); 5 while (work_queue.empty () 6 {7 uv_cond_wait (& cv, & mutex); 8} 9 WorkPtr work; 10 work. swap (work_queue.front (); 11 work_queue.pop (); 12 uv_mutex_unlock (& mutex); 13 // doing work... 14}