?
Solved

Thread Model for Interactive server

Posted on 2011-09-11
5
Medium Priority
?
406 Views
Last Modified: 2012-05-12
Hi:

I was creating a deamon...It is going to be multi-threaded.....I wanted to validate a thread model for it.

After accepting a socket connecting

client_socket = accept(server_socket,&addr,&len);

I create a sessionhandler which will process the incoming data, My session handler is created with incoming with socket created by accept call:

sessionhandler = new sessionhandler(client_socket);

Then I create a thread....

hThread = CreateThread(
                  NULL,                        
                  0,                            
                  Connection,                  
                  (void *)sessionhandler,                
                  0,                           /
                                );  

Now connection function processes incoming packets.....
Connection function has the following code fragment...........

while(len=recv(sessionhandler->Connection,buf,sizeof(buf),0))
      {
            if(-1==sessionhandler->Process(buf,len))
            {
                              
            }
                  }      
      
Is this Thread model fine....for medium scale bursty processing of max 100 to 200 clients at peak simultaneously......

Each user access his own resource but there might be more than one session per user.....

Here is the code flow:  

A processing class gets created for each connection......

A thread also get created with each connection......

This thread is associated with a particular class handle....which is not unique in code....

Thread is associated with particular object and connection function. connection function is in server-main.cpp processing class/object is separate.

Will there be unique processing object for each socket connection and thread created....

Please let me know if this would work. If you could suggest improvements it is fine.....

If you suggest a better thread model it is also fine....

I am using g++ compiler on Ubuntu platform and POSIX thread library....

Look forward to your responses.....

Best Regards,

-Sunnybrad
0
Comment
Question by:sunnybrad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 36518556
I'd improve your approach by using a worker thread pool, rather than creating a new thread for every connection. You'll avoid quite a bit of thread creation overhead that way.

Other than that, what you describe is a pretty standard approach to writing a server.
0
 

Author Comment

by:sunnybrad
ID: 36519147
Hi Infinity08:

There is authentication required so I guess I have to maintain one thread per one client connection.

How can I create a worker thread pool todo this......

Worker thread pool does not know anything about client authentication details....

If I can do this via a worker thread pool please explain with a small code fragment.

Thanks

-Sunnybrad
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 36519262
A thread pool is simply a system that manages threads. You request a thread from it, and it will return you one that is available (unused), or if none are available, it creates a new thread and returns that. You can then let the thread do some work, and when the work is done, the thread becomes available again in the thread pool. A subsequent request can then re-use that same thread without having to start a new one.

The kind of work you do in the thread is unimportant to the thread pool. So, you can do authentication, or anything else you need.

I recommend you read up on the concept of thread pools - here eg. : http://en.wikipedia.org/wiki/Thread_pool_pattern

And then you can look for a specific thread pool implementation that suits your needs, or roll your own.


An alternative approach could be to use boost asio (http://www.boost.org/doc/libs/release/doc/html/boost_asio.html) ... Have a look at that if you like the idea :)
0
 

Author Comment

by:sunnybrad
ID: 36519344
Hi Infinity08:

I like the idea of thread pool and will try it out..

As for Boost....I guess it has libraries to do things....I had two question regarding that.....

Does using Boost have licensing implementation.....for commercial products....

As compared to elementally developing using some other thread libraries in Linux environment will using boost increase final package size etc....

Thanks for all your responses....really helpful....

-Sunnybrad
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 36521014
>> Does using Boost have licensing implementation.....for commercial products....

The boost license can be read here : http://www.boost.org/users/license.html
You'll find that there is basically no restriction to its usage in a commercial product (except for specific cases detailed in the license).


>> As compared to elementally developing using some other thread libraries in Linux environment will using boost increase final package size etc....

That's difficult to answer with certainty. You'll have to try and see for yourself.

One thing that is sure though, is that the boost libraries have been written with a lot of care for performance and ease of use.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question