By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

parallel, concurrent server with Boost::thread

P: 2
Greetings to everyone...
This is my first message in this forum and I have a very short experience in C++.
This said, I would like to setup a server that accept strings from many clients, elaborates those strings and send them back to each client, in the correct way.

The elaboration is done through and external program, that I have to call with "execl", this is compulsory. So this program is a shared resource that should be "mutexed", in order to avoid wrong order in messages.

With a simple C syntax, I would like to so something like this:

Expand|Select|Wrap|Line Numbers
  1. //parent process
  2. while(1){
  3.         int conn=accept(listen_sd,........);
  4.  
  5.         if( fork()==0 ){  //child process
  6.              close(listen_sd);
  7.              //reading from conn
  8.              //elaborating received data, through a common resource, to be "mutexed"
  9.              //sending results to conn
  10.         }
  11.  
  12.         //parent process
  13.         close(conn);
  14. }
The basic idea is to open a new thread for each new client connected, and every thread has to be independent and to be synchronized to the common resource.

Is there someone so kind and powerful who could help me in a practical and easy way?
(I am sorry but I'm using C++ only for 4 month and many things are still so hard)

Thank you all for your attention
EMA
Jun 8 '07 #1
Share this Question
Share on Google+
3 Replies


gpraghuram
Expert 100+
P: 1,275
HI,
You can use posix multithraeding.
You have to create a detached thread for each client as soon as you accept the connection.

Hope this helps
Raghuram
Jun 8 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This is not a C++ issue. This is a multithreading issue.

I suspect you will call pcreate_thread and fork your process on that thread.

I would start with a book on multithreading.
Jun 8 '07 #3

P: 2
Thank you for your replies...
Actually I am still studying C++ (or C for networking), I am mainly learning through examples because I have no time to study deeply...
I would like to say that I could perfectly setup such a system in Java, it would be pretty easier, but I cannot use it here...

So my problem is not just "multithreading" itself, but how to realize it in a real way in C++, if possible.
I though to Boost::thread for there is some "simple" example, I took a look to Posix threads but they seems really complicated for me...

Can you suggest me some practical application to manage synchronization, in C++?

Thank you very much for you patience...
Jun 9 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.