Exchanging data betwwen threads?

Discussion in 'C++' started by Solang, Sep 20, 2009.

  1. Solang

    Solang Guest

    I have 3 threads A,B and C;
    Thread A handle all GUI while thread B keep generating data(about 10K at a
    time) and
    supposedly pass that data to thread A. Everytime thread A get that data, it
    also passes them to
    thread C.

    How fo I pass those data between threads?
     
    Solang, Sep 20, 2009
    #1
    1. Advertising

  2. "Solang" <> wrote in message
    news:DHotm.3060$...
    >I have 3 threads A,B and C;
    > Thread A handle all GUI while thread B keep generating data(about 10K at a
    > time) and
    > supposedly pass that data to thread A. Everytime thread A get that data,
    > it also passes them to
    > thread C.
    >
    > How fo I pass those data between threads?


    Ideally, put a single producer/consumer queue between B-A, and another
    single producer/consumer queue between A-C.
     
    Chris M. Thomasson, Sep 20, 2009
    #2
    1. Advertising

  3. "Sam" <> wrote in message
    news:-scan.com...

    > The lowest common denominator is the POSIX threads, supported on most
    > platforms. There are many ways to use POSIX threads. The most elemental
    > approach is to declare a couple of objects in the static scope: a mutex, a
    > condition variable, and the object being passed between threads. The mutex
    > protects access to the object, and the condition variable serves to notify
    > the interested threads. Bringing in STL into the picture, a std::list can
    > often be used as a convenient way to pass objects between threads. The
    > sending thread would acquire the mutex, add the object to be send to the
    > std::list, signal the condition variable, and release the mutex.


    FWIW, it can be beneficial to signal the condition variable after releasing
    the mutex.




    > A receiving
    > thread would lock the mutex, check the std::list and wait on the condition
    > variable if it's empty, remove the object from the std::list, and release
    > the mutex. But that's just one, basic approach. Many other more
    > sophisticated designs are also possible.


    Indeed.
     
    Chris M. Thomasson, Sep 20, 2009
    #3
  4. "Solang" <> wrote in message
    news:DHotm.3060$...
    >I have 3 threads A,B and C;
    > Thread A handle all GUI while thread B keep generating data(about 10K at a
    > time) and
    > supposedly pass that data to thread A. Everytime thread A get that data,
    > it also passes them to
    > thread C.
    >
    > How fo I pass those data between threads?


    A simple method is to create a queue; example pseudo-code typed in news
    reader, please forgive any typos:
    _______________________________________________________________________
    struct node
    {
    node* m_next;
    };




    class queue
    {
    node* m_head;
    node* m_tail;


    public:
    queue()
    : m_head(NULL)
    {

    }


    public:
    void push(node* n)
    {
    n->m_next = NULL;

    if (! m_head)
    {
    m_head = n;
    }

    else
    {
    m_tail->m_next = n;
    }

    m_tail = n;
    }


    node* pop()
    {
    node* n = m_head;

    if (n)
    {
    m_head = n->m_next;
    }

    return n;
    }
    };




    class thread_safe_queue
    {
    mutex m_mutex;
    cond m_cond;
    queue m_queue;


    public:
    void push(node* n)
    {
    {
    mutex::guard lock(m_mutex);

    m_queue.push(n);
    }

    m_cond.signal();
    }


    node* pop()
    {
    node* n;

    {
    mutex::guard lock(m_mutex);

    while (! (n = m_queue.pop()))
    {
    m_cond.wait(lock);
    }
    }

    return n;
    }
    };
    _______________________________________________________________________




    Or, if you prefer semaphores:
    _______________________________________________________________________
    class thread_safe_queue
    {
    mutex m_mutex;
    semaphore m_sem;
    queue m_queue;


    public:
    void push(node* n)
    {
    {
    mutex::guard lock(m_mutex);

    m_queue.push(n);
    }

    m_sem.post();
    }


    node* pop()
    {
    m_sem.wait();
    mutex::guard lock(m_mutex);

    return m_queue.pop();
    }
    };
    _______________________________________________________________________
     
    Chris M. Thomasson, Sep 20, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    5
    Views:
    526
    Shezan Baig
    Jan 30, 2005
  2. Amol
    Replies:
    2
    Views:
    454
    Valentin Tihomirov
    Oct 12, 2003
  3. Al
    Replies:
    2
    Views:
    450
  4. thinktwice
    Replies:
    2
    Views:
    803
    thinktwice
    Aug 25, 2006
  5. Replies:
    2
    Views:
    100
Loading...

Share This Page