Exchanging data betwwen threads?

S

Solang

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?
 
C

Chris M. Thomasson

Solang said:
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.
 
C

Chris M. Thomasson

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

Chris M. Thomasson

Solang said:
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();
}
};
_______________________________________________________________________
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top