I am developing an application in which I do multithreading. One of my worker threads displays images on the widget. Another thread plays sound. I want to stop/suspend/pause/sleep the threads on a button click event. It is same as when we click on video player play/pause button. I am developing my application in c++ on linux platform using the pthread library for threading.
Can somebody tell me how I achieve threads pause/suspend?
You can use a mutex, condition variable, and a shared flag variable to do this. Let's assume these are defined globally:
You could structure your playback code like this:
Then, to play you can do this:
And to pause:
You have your threads poll for "messages" from the UI at regular interval. In other words, UI in one thread posts action messages to the worker threads e.g. audio/video.