I am still working on my realtime application for my car that will be running on Minimal Ubuntu and the gtkmm library. My current application runs alright for a singly thread application. I am in the process of multi-threading it (still learning threads). What I thought would be the best way to rewrite the back end was using a single queue shared across three threads. Someone suggested a better method where I would have a circular buffer in each hardware communication thread. The somehow communicate with the circular queues from the main thread to get the oldest data.

My question is how can I communicate from the main thread to a worker to get data from the buffer in a lock less or in the least amount of locks using either pthreads or glibmm::threads?

You will have to implement some sort of thread synchronization scheme in order for the threads to access a single commen queue to prevent corruption of the queue data. That means only one thread can access the shared common queue at a time -- all other threads that want to access it will have to wait their turn. If you are not very careful that could cause a deadlock situation.

Creating and using a mutex is one way to do it, assuming your os supports them. Since it supports multithreading then I would imagine it would also support mutexes.

My question is how can I communicate from the main thread to a worker to get data from the buffer in a lock less or in the least amount of locks using either pthreads or glibmm::threads

About all that the main thread can do is set a global variable that indicates new data is available in the queue. Then the threads check the status of that variable and act accordingly. Another possibility is to use signals -- main sets a signal and the threads catch it???? I don't know if that will work or not.

Edited 6 Years Ago by Ancient Dragon: n/a

Thanks for the info. I'll try to do it the easy way by using mutexs and see if that meets my needs.

This article has been dead for over six months. Start a new discussion instead.