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. [SOLVED]How to share data between threads
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]How to share data between threads

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 6.0k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    Jake007
    wrote on 10 Aug 2013, 16:37 last edited by
    #1

    Hi!

    I'm trying to setup a communication between 2 threads.
    First thread is generating data and sending it to a second one. Second thread then merges as much data as possible in the given time and sends it to a GUI to be displayed. Given time is a few milliseconds so that GUI doesn't crashes. Both threads have while (true) condition. I cannot use Qt::QuedConnection because of that
    and Qt::DirectConnection causes SIGSEGV when data is accessed from both threads at the same time.

    Data = QList<QString>

    First threads appends. Seconds thread reads and removes.
    How can I achieve stable communication? I tried with QMutex, but with no effect.

    Thanks in advance!

    Regards,
    Jake


    Code is poetry

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on 10 Aug 2013, 17:02 last edited by
      #2

      Sounds like you have a classical producer/consumer problem here, which can be solved using a shared (ring) Buffer plus two Semaphores and a Mutex.

      See:
      "Producer-Consumer Problem":http://tinyurl.com/kls77un

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on 10 Aug 2013, 17:13 last edited by
        #3

        Qt Example:

        @QMutex mutex;
        QSemaphore semFree(8);
        QSemaphore semAvail(0);

        QList<MyData> buffer;

        QProducerThread::run()
        {
        while(true)
        {
        MyData data = produceItem();
        semFree.acquire();
        mutex.lock();
        buffer.append(data);
        mutex.unlock();
        semAvail.release();
        }
        }

        QConsumerThread::run()
        {
        while(true)
        {
        MyData data;
        semAvail.acquire();
        mutex.lock();
        data = buffer.takeFirst();
        mutex.unlock();
        semFree.release();
        doSomethingWith(data);
        }
        }@

        --

        BTW: The second thread (consumer thread) should definitely use a queued connection to send the data to the GUI thread (event loop). The corresponding slot can then update the GUI (from the context of the "main" thread). Trying to access any GUI controls from a "background" thread will almost certainly lead to undefined behavior including crash...

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jake007
          wrote on 11 Aug 2013, 12:27 last edited by
          #4

          Thanks! It works.

          Seconds thread is using queued connection with additional delay (10 milliseconds I think) just to keep GUI responsive.


          Code is poetry

          1 Reply Last reply
          0

          1/4

          10 Aug 2013, 16:37

          • Login

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