Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem while moving an object to a new thread

Problem while moving an object to a new thread

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sayem.bd
    wrote on 27 Oct 2011, 06:19 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on 27 Oct 2011, 08:10 last edited by
      #2

      Read about "objects and threading on the wiki":https://developer.qt.nokia.com/wiki/ThreadsEventsQObjects.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sayem.bd
        wrote on 30 Oct 2011, 15:00 last edited by
        #3

        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).

        1 Reply Last reply
        0

        1/3

        27 Oct 2011, 06:19

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved