473,811 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about multi-threading

Hello. I have a doubt about multi-threading, if it is or not the best way to
achieve the following:

I have a server application that listens a port through a socket.
A client will send messages to that application (many messages).
Because the amount of messages is huge, the server application should only
send each received message to another process (a thread?) that manages the
message, and then go with the next message.

I want a limited number of threads, so the server app just looks for one
that is free and send the message to it. Once the threads finish the work,
they wait to be called by the server app.

Are threads the solution for that?


Regards,

Diego F.
Apr 25 '07 #1
7 1710
Diego,

Yes, I believe they are, but you will need to hold off on accepting new
or processing new messages (the semantics are up to you) until the previous
ones complete.

Are you locked into the protocol that you are using for requests from
the clients, or are you able to dictate what the message format is? If so,
I would recommend using WCF to handle the communications (you can use a Tcp
connection for the transport and a binary encoder for the format of the
messages) as it has this kind of tuning built in.

If the format of the messages is locked, and you have to implement this
custom, you still might want to create a custom transport in WCF on the
service side as well as a custom encoder, and allowing WCF to handle all the
plumbing involving threading and the like.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Diego F." <di********@msn .comwrote in message
news:OC******** ******@TK2MSFTN GP03.phx.gbl...
Hello. I have a doubt about multi-threading, if it is or not the best way
to achieve the following:

I have a server application that listens a port through a socket.
A client will send messages to that application (many messages).
Because the amount of messages is huge, the server application should only
send each received message to another process (a thread?) that manages the
message, and then go with the next message.

I want a limited number of threads, so the server app just looks for one
that is free and send the message to it. Once the threads finish the work,
they wait to be called by the server app.

Are threads the solution for that?


Regards,

Diego F.

Apr 25 '07 #2
"Diego F." <di********@msn .comwrote in message
news:OC******** ******@TK2MSFTN GP03.phx.gbl...
I have a server application that listens a port through a socket.
A client will send messages to that application (many messages).
Because the amount of messages is huge, the server application should only
send each received message to another process (a thread?) that manages the
message, and then go with the next message.

I want a limited number of threads, so the server app just looks for one
that is free and send the message to it. Once the threads finish the work,
they wait to be called by the server app.

Are threads the solution for that?
Sure. Rather than "send each received message to another process", I might
write "enqueue each received message to another thread".

Assuming that the order of processing is unimportant, it seems to me that an
easy solution is to use the BackgroundWorke r class. With this, .NET manages
your pool of threads. You can add a task to the pool, and if there's a
thread available to work on the task, it is assigned the task and runs. If
all of the threads are already busy, your task is kept in a queue until
there's a thread available.

In this paradigm, a "task" is represented simply as a delegate which you add
to the BackgroundWorke r's DoWork event. You can pass unique data to the
RunWorkerAsync( ) method, and extract that data from the Argument property of
the DoWorkEventArgs passed into your DoWork delegate. The call to
RunWorkerAsync( ) enqueues your task, and when there's a thread available to
run the task, your DoWork delegate is called for the actual processing.

I read the reply from Nicholas recommending the use of WCF for this purpose.
I have to admit, I don't know anything about WCF and maybe it has this sort
of thing built right into it. If it is, maybe Nicholas could be a bit more
explicit as to what part of WCF would be useful for doing this sort of
data-processing delegation.

Pete

Apr 25 '07 #3
Peter,

Assuming that the OP can rewire the client and server to use WCF, it
would be a good fit here. In particular, I'm assuming that the OP would
want to use the TCP binding in WCF.

With WCF, at least for the Tcp binding, you can set the ListenBacklog
and MaxPendingAccep ts properties in the config file (and through code, if
you prefer).

The ListenBacklog property will allow you to set the maximum number of
queued connection requests that can be pending, while the MaxPendingAccep ts
property indicates the maximum number of concurrent accepting threads on the
endpoint will have.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.com...
"Diego F." <di********@msn .comwrote in message
news:OC******** ******@TK2MSFTN GP03.phx.gbl...
>I have a server application that listens a port through a socket.
A client will send messages to that application (many messages).
Because the amount of messages is huge, the server application should
only send each received message to another process (a thread?) that
manages the message, and then go with the next message.

I want a limited number of threads, so the server app just looks for one
that is free and send the message to it. Once the threads finish the
work, they wait to be called by the server app.

Are threads the solution for that?

Sure. Rather than "send each received message to another process", I
might write "enqueue each received message to another thread".

Assuming that the order of processing is unimportant, it seems to me that
an easy solution is to use the BackgroundWorke r class. With this, .NET
manages your pool of threads. You can add a task to the pool, and if
there's a thread available to work on the task, it is assigned the task
and runs. If all of the threads are already busy, your task is kept in a
queue until there's a thread available.

In this paradigm, a "task" is represented simply as a delegate which you
add to the BackgroundWorke r's DoWork event. You can pass unique data to
the RunWorkerAsync( ) method, and extract that data from the Argument
property of the DoWorkEventArgs passed into your DoWork delegate. The
call to RunWorkerAsync( ) enqueues your task, and when there's a thread
available to run the task, your DoWork delegate is called for the actual
processing.

I read the reply from Nicholas recommending the use of WCF for this
purpose. I have to admit, I don't know anything about WCF and maybe it has
this sort of thing built right into it. If it is, maybe Nicholas could be
a bit more explicit as to what part of WCF would be useful for doing this
sort of data-processing delegation.

Pete

Apr 25 '07 #4
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:uJ******** ******@TK2MSFTN GP04.phx.gbl...
[...]
The ListenBacklog property will allow you to set the maximum number of
queued connection requests that can be pending, while the
MaxPendingAccep ts property indicates the maximum number of concurrent
accepting threads on the endpoint will have.
It's not clear to me that that's what the OP wants. You seem to be talking
about having control over the lower-level aspects of making TCP connections,
while the OP's question seems to me to just be talking about separating his
i/o (the network communications) from the processing (the act of "managing
the message").

Do I misunderstand the original post, or is there something in WCF that is
particularly suited to addressing the latter?

Pete

Apr 25 '07 #5
Peter,

The OP wants to have a limited number of threads to process incoming
requests that come in on a port. This is what WCF is doing with the
MaxPendingAccep ts property. It basically says "hey, you can have

The thing is, while the client is only going to process a limited number
of calls, the OP will still have to worry about queuing the incoming calls
that will come in while his service is running. Then, when the operations
are done, the OP also has to worry about dispatching those requests.

That's where the ListenBacklog property will come in, as it will
indicate how many pending requests can be backlogged before requests are
starting to be turned away (the OP might want to turn this way up).

The reason to recommend WCF in this case is because it handles all of
this for you. You just have to define your interface, and code against it
(within the model, of course). WCF will handle listening for the request,
managing how many requests are processed at the same time, denying other
requests if too many are coming in, etc, etc for you. The OP is going to
have to worry about this himself if he codes it from scratch.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.com...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:uJ******** ******@TK2MSFTN GP04.phx.gbl...
>[...]
The ListenBacklog property will allow you to set the maximum number of
queued connection requests that can be pending, while the
MaxPendingAcce pts property indicates the maximum number of concurrent
accepting threads on the endpoint will have.

It's not clear to me that that's what the OP wants. You seem to be
talking about having control over the lower-level aspects of making TCP
connections, while the OP's question seems to me to just be talking about
separating his i/o (the network communications) from the processing (the
act of "managing the message").

Do I misunderstand the original post, or is there something in WCF that is
particularly suited to addressing the latter?

Pete

Apr 25 '07 #6
There are a number of different architectures available for you to pick
from. In general, the best architecture for building a "big" socket
application is to use Async Sockets, and let Windows (and the CLR) manage
the majority of your threading for you. If possible, you should avoid
creating and managing your own threads as there's alot of complexity
inherent in doing so.

I build big socket servers for a living, and some time ago I wrote up a
bunch of the architectural iterations that I've been through when doing this
in .Net.

http://www.coversant.com/Coversant/B...0/Default.aspx

--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Diego F." <di********@msn .comwrote in message
news:OC******** ******@TK2MSFTN GP03.phx.gbl...
Hello. I have a doubt about multi-threading, if it is or not the best way
to achieve the following:

I have a server application that listens a port through a socket.
A client will send messages to that application (many messages).
Because the amount of messages is huge, the server application should only
send each received message to another process (a thread?) that manages the
message, and then go with the next message.

I want a limited number of threads, so the server app just looks for one
that is free and send the message to it. Once the threads finish the work,
they wait to be called by the server app.

Are threads the solution for that?


Regards,

Diego F.

Apr 25 '07 #7
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:%2******** **********@TK2M SFTNGP03.phx.gb l...
Peter,

The OP wants to have a limited number of threads to process incoming
requests that come in on a port. This is what WCF is doing with the
MaxPendingAccep ts property. It basically says "hey, you can have
I think you left out some words. :)

As far as "wants to have a limited number of threads to process incoming
requests that come in on a port" goes...

I admit to not being familiar with WCF, but if I read the docs right, using
the ListenBackLog property assumes that each request is made with a new TCP
connection. It's equivalent to the "backlog" parameter of Socket.Listen() .
I don't read the original post as asking for that functionality (and
frankly, I don't really see how WCF provides significantly easier access to
that functionality than simply setting it using the plain Socket.Listen()
method).

I guess only the OP can really clear this up, as it comes down to a
disagreement as to what he actually meant. Hopefully between the two of us,
someone's provided him with a useful answer. :)

Pete

Apr 25 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2238
by: Adam Hartshorne | last post by:
The input to a function of a 3rd party library I want to use requires a double**, which is a multi-dimension array of doubles. I have looked on the net etc and seen several ways of supposedly doing this, but I don't seem to be able to get them to work. I was wondering if anybody can tell me what I am doing wrong. int rows = 10 ; int cols = 10 ;
1
1923
by: herrcho | last post by:
#include <stdio.h> int multi; int main() { printf("\nmulti = %u",multi); printf("\nmulti = %u",multi); printf("\n&multi = %u\n",&multi); return 0;
2
11277
by: herrcho | last post by:
#include <stdio.h> int multi; int main() { printf("\nmulti = %p",(void *)multi); printf("\nmulti = %p",(void *)&multi); printf("\nmulti = %p",(void *)multi); printf("\n&multi = %p\n",(void *)&multi);
13
3267
by: John Salerno | last post by:
Hi all. I have a question about interfaces now. According to the book I'm reading, when you implement an interface, the class or structure has to declare all the methods that the interface includes. My question is, if you must declare each method (as opposed to simply 'using' those methods like an #included file), then why not just declare the methods on your own as if you created them yourself? What advantage does using an interface...
28
2089
by: jakk | last post by:
Hello All, I have a question about how to handle exceptions. Iam working on an ASP.NET application which has a presentation layer, Business Layer and DataAccess Layer. Iam confused about where to handle exceptions. Say forexample I have a requirement where certain value has to be between 30-72 only and Iam doing this validation in the the BL. if the value is not between 30-72 then Iam throwing an exception back to the UI layer. Is this...
0
1950
by: Kaimar Seljamäe | last post by:
Hi, I have to create a web service client which uses SOAP encoding but does not use "multi-reference" values (see http://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383513 item 10). If I create SOAP client like this: public class StockService : SoapHttpClientProtocol {
1
1343
by: Frank Millman | last post by:
Hi all I am developing a multi-user business/accounting application. It is coming along nicely :-), though rather slowly :-( I have hit an issue which will require a lot of changes to the code I have written so far, together with an increase in complexity and all the bad things that follow from that. Before I go ahead and make the changes, I thought I would bounce it off the group and see if there is a simpler approach.
5
606
by: Tom | last post by:
Using multiple System.Timers.Timer objects in a Windows Service for performing multi-thread activities in a periodic fashion. Timers are AutoReset=false, to only have a single timer execution thread running at any given moment. Typically, timers will have an interval of 5 to 15 minutes. Conditionally, the interval may be set to 1 second (1000 ms) before the next call to Timer.Start(). Here is the question: if the interval is one...
13
2079
by: ARC | last post by:
Hello all, Prior to going live with my app, I have questions on relationships theory. My prior app was done in Access 97, and I did NOT use relationships at all. I have 65 tables in my back-end database, and with Access 97, if you added relationships, it would add multiple copies of the same relationships over and over, so I decided not to use any relationships at all. Additionally, when I needed to add or remove fields via code, it...
14
2125
by: Alexander Dong Back Kim | last post by:
Dear all, I used to use C++ programming language at all time but moved to C# and Java. Few days ago, I restarted studying about C++ with a very beginner's mind. I wrote a simple class and gcc couldn't compile the class. Any hints that I'm missing? Header File: #ifndef __Calc_h__
0
9731
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4342
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.