Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Problem while moving an object to a new thread
sayem.bd last edited by
Hi, I have a class named "AudioEngine":http://pastebin.com/QFg2kL9h which manages an "AudioRecorder":http://pastebin.com/g1g8Kkbg object to fetch data from an audio input. It has an instance variable named sendingBuffer which serves as a circular buffer for the audio recorder (the buffer class is "here":http://pastebin.com/aRudJui7). I have used "QAudioInput":http://doc.qt.nokia.com/4.7/qaudioinput.html for fetching data from the audio device which is a member of the AudioRecorder object.
Whenever an user presses a button, it causes a method of AudioEngine to execute which in turns starts a new thread (recorderThread). The AudioRecorder is moved to this thread by calling the moveToThread method (in the constructor of the AudioEngine).
When the thread starts, it emits a signal which is picked up by the AudioRecorder instance.
The AudioRecorder instance then starts the QAudioInput instance by calling its start method, which in turn spawns a new internal thread which starts fetching data from the audio device.
When data is available on the device buffer, this internal thread then calls the writeData method of the specified buffer. I have overloaded this method in my buffer implementation to store the data in a character array.
The problem is this - writeData method is being executed in the main thread (which I checked while debugging) of the application. I don't want that because when my character array becomes full, I will make the current thread to sleep until it becomes free. If I make the main thread to sleep, then it may in turn freeze my UI. I want it to execute in the same thread in which the QAudioInput lives. Since its parent is AudioRecorder and I have moved it to my new thread, QAudioInput is obviously moved to the new thread. But still the writeData method is being executed in the main thread.
Why is this happening? How can I make sure that the writeData method gets executed in my recorderThread?
Franzk last edited by
Read about "objects and threading on the wiki":https://developer.qt.nokia.com/wiki/ThreadsEventsQObjects.
sayem.bd last edited by
Thank you Franzk for this wonderful link. It's quite insightful although it didn't solve my problem. However, as a workaround I have used pull strategy to pull data out of the audio device into my buffer which resulted in the behavior which I wanted (I still have no clue what happened though).