문제

I have an application capturing sampled data and writing the samples into a shared memory segment structured as a circular buffer. I have a 2nd application reading the shared memory. All of the shared memory is using a mutex for proper serialization. I have complete control over the 1st application for starting and stooping the capture process using a msg queue channel for control.

My question is regarding the proper way to architect the read thread of the 2nd application that will read the shared memory segment to process the data. Currently I have written basic test routines to verify that the reading of shared memory is accurate and that the mutex is functioning correctly. But I am not really sure of how best to design the actual reading thread for the 2nd application.

I really like the simplicity of a dedicated pthread that uses select() to block until data is ready to be read. I use this design a lot, but this approach does not seem to fit when using shared memory because a file descriptor is not associated with the shared memory segment so I cannot use select(). For some reason using a pthread with a while loop with a periodic call to sleep() just does not seem like the proper way to do this.

So my question is what is the elegant way to architect the read thread to block until data has been written into the shared memory by the 1st application? The only way I can think of is to use an additional msg queue to send the 2nd application a msg each time I want it to read a buffer from the shared memory segment. Is there a more elegant way?

Thanks, -Andres

도움이 되었습니까?

해결책

You have a lot of choices. You can use a process-shared condition variable along with your process-shared mutex, you can use a futex, you can use a pipe, or you can use a semaphore. Typically, a process-shared condition variable and mutex in the shared memory is your best choice.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top