1

I have created a Broadcast Server which must 10 clients at a time. It must be able to read data from 10 Clients at a time and reply through a Broadcast Message to all the Clients. To make this communication fast, I have decided to make two Separate Threads at Server Side.

One Thread1 would be dedicated for Continuously receiving data and placing the data in a queue. Then this data is read from the queue by another Thread2 and processing is done.

i.e. the queue must be shared between Thread1 and Thread2.

How can I do it efficiently? How a queue is shared between these two threads?

And what if Thread1 is writing in the same Queue and Thread2 is reading?

Any help will be appreciated.

Mallikarjuna Reddy
  • 1,212
  • 2
  • 20
  • 33
Ayse
  • 2,676
  • 10
  • 36
  • 61
  • 1
    To share data between threads, please take a look at this [question](http://stackoverflow.com/questions/118199/c-thread-shared-data). To prevent threads from reading and writing to the same shared data, you need to [synchronize](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686689%28v=vs.85%29.aspx) access to the share data. – Karim ElDeeb Mar 15 '13 at 11:05
  • I'll let you know how it worked once I try it, but looks helpful :) Thanks :) – Ayse Mar 15 '13 at 11:15

1 Answers1

0

The main idea is that the "consumer" waits until some data is available, and the "producer" notifies when data is available. This can be done simply and efficiently using condition variables. Take a look at this article - the author implements such a queue, accompanying every step with a detailed explanation.

Igor R.
  • 14,716
  • 2
  • 49
  • 83