简体   繁体   中英

how to organize waiting for data from main thread in consumer thread

Main thread reads data from sockets and stores it in queue. Second thread write this data to disk file. But if second writes faster than first reads data, then how to organize waiting.

I wanna do it with semaphore, but how i can increase semaphore counter then next data chunk reads into queue. How to decreasde it in second thread i know.

Sounds like a 'classic' producer-consumer queue. You need a thread-safe queue and a semaphore, initialized to 0, to count objects in it. If you have no thread-safe queue, another semaphore initialized to 1, (or a mutex), will do to protect the queue from multiple access.

Essentially, in the producer, malloc a buffer struct, read data from socket into it, push its address onto the thread-safe queue, signal the semaphore. In the consumer, wait on the semaphore, pop a buffer address from the queue, write the data to your disk file, free the buffer.

You could, with C++, substitute a buffer class and new/dispose for the buffer struct and malloc/free. The socket/disk operations could then be methods of the buffer class.

Rgds, Martin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM