Vikram Ranabhatt
Vikram Ranabhatt

Reputation: 7610

passing information to multiple thread

I am working on MultiThreaded TCP Server(c++) which will serve multiple client to stream Video. I have decided to go with "thread per client" approach. There're no more than 100 simultaneous clients.

I am multiplexing the client using select system call and accepting the (accept) and then creating thread to stream video.

  1. Is this Correct approach in this scenario?
  2. I need to pass certain information like (socket id and handle to thread,threadID etc and other information to steam the video). How can i pass these information to all the thread, as these information are not common to all thread. I dont want to take array of HANDLE for the thread and thread id.

Upvotes: 0

Views: 215

Answers (3)

Andriy Tylychko
Andriy Tylychko

Reputation: 16256

"Thread per client" is not scalable approach, especially for video streaming server (usually high-performance required). Have a look at Boost.Asio library, especially at example "An HTTP server using a single io_service and a thread pool calling io_service::run()." (just ignore HTTP-specific stuff). Using Boost.Asio you'll receive high-performance scalable multithreaded server w/o headache.

Upvotes: 0

ravenspoint
ravenspoint

Reputation: 20457

Please don'r "reinvent the wheel".

For example, the boost::asio library gives enormous help in creating a server such as you describe. There are lots of tutorials and examples - perhaps the most immediately useful to you would be http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp

Upvotes: 4

bua
bua

Reputation: 4870

usually starting a thread,
ether using standard approach of void function or functor class:

class StreamInfo
{
   public:
      int _id;
      std::string _ip;
      int _port;
      int status;
      StreamInfo(int id, string ip, int p):_id(id), _ip(ip), _port(p){};
};

void myThread(void* datastruct)
{
      StreamInfo* info = static_cast<StreamInfo*>(datastruct);
      ..
      ..
      //busy wait..
      while(...)
      {
          // check for some new info
          if(info->status)....
       }
}

main(..)
{
    multimap<int, StreamInfo*> mmap;
    SOCK s=null;
    StramInfo* i=null;
    while(s = accept(sock))
    {
       i=new StreamInfo(12, "localhost", 5000);
       id = createThread(...myThread, i);
       ....
       mmap.append(pair<..,..>(id, i);
    };

};

//...some other thread
/// change StreamInfo status for thread 12
mmap.last(12).status = 134;
//... then thread 12 will automatically recon this change in busy wait.

its just one approach, you could use observer pattern as well. But this would require more work.

I imagine streaming is quite bandwidth, cpu and memory consuming.
So switching between threads might be not efficient enough though.
Maybe look at the asynchronous approach.

Boost::ASIO is pretty decent for that purpose and works on all platforms.

Upvotes: 0

Related Questions