Difference between revisions of "FAQ.MultiThreading"

From Overbyte
Jump to navigation Jump to search
Line 4: Line 4:
 
When dealing with applications that need several connections, often using threads come up. Why that ? Mainly because the idea to use (if it's a client) or to serve (if it's a server) several sockets - ie several connections - means using or serving them concurently. And, the easiest way to understand "concurently" is to say "doing several thing at the same time" and them comes up the Threads, the multitasking, etc...
 
When dealing with applications that need several connections, often using threads come up. Why that ? Mainly because the idea to use (if it's a client) or to serve (if it's a server) several sockets - ie several connections - means using or serving them concurently. And, the easiest way to understand "concurently" is to say "doing several thing at the same time" and them comes up the Threads, the multitasking, etc...
  
Although with ICS it's not '''primarily''' necessary, as ICS is based on the [[Asynchronous_Paradigm | Asynchronous Paradigm]], sometimes it may be needed to use threads.
+
Although with ICS it's not '''primarily''' necessary, as ICS is based on the [[Asynchronous_Paradigm | Asynchronous Paradigm]], sometimes threads have to be used.
  
 
But before to see why and how to use threads with ICS, let's define what '''is''' a thread : ''"Threads are a way for a program to split itself into two or more simultaneously running tasks <nowiki>[...]</nowiki> Threads are similar to processes, but differ in the way that they share resources "'' ([http://en.wikipedia.org/wiki/Thread_%28computer_science%29  Wikipedia's Thread (computer_science)]). By extension, MultiThreading is the ability for a processus to have several Threads.
 
But before to see why and how to use threads with ICS, let's define what '''is''' a thread : ''"Threads are a way for a program to split itself into two or more simultaneously running tasks <nowiki>[...]</nowiki> Threads are similar to processes, but differ in the way that they share resources "'' ([http://en.wikipedia.org/wiki/Thread_%28computer_science%29  Wikipedia's Thread (computer_science)]). By extension, MultiThreading is the ability for a processus to have several Threads.

Revision as of 20:10, 3 March 2006

Main page -> FAQ -> MultiThreading

Introduction

When dealing with applications that need several connections, often using threads come up. Why that ? Mainly because the idea to use (if it's a client) or to serve (if it's a server) several sockets - ie several connections - means using or serving them concurently. And, the easiest way to understand "concurently" is to say "doing several thing at the same time" and them comes up the Threads, the multitasking, etc...

Although with ICS it's not primarily necessary, as ICS is based on the Asynchronous Paradigm, sometimes threads have to be used.

But before to see why and how to use threads with ICS, let's define what is a thread : "Threads are a way for a program to split itself into two or more simultaneously running tasks [...] Threads are similar to processes, but differ in the way that they share resources " (Wikipedia's Thread (computer_science)). By extension, MultiThreading is the ability for a processus to have several Threads.

  • Advantages :
    • Not to freeze the other threads
    • run concurently with other threads
  • Disadvantages
    • resources sharing has to be managed carefully (concurent access)
    • With Delphi, VCL - ie all the GUI stuff - is not Thread safe.

The Main Question

Do i need Threads ?

Cases where needed and where not needed

A Thread per Socket

When incoming connection, encapsulating socket within a thread, and managing communication within the thread, across several threads

Many sockets per thread

A single thread is able to handle a lot of simultaneous sockets. Using a single socket per thread is not the way to go to support a large number of concurrent connections. In that case you need to use many socket per threads. Something like 10 to 500 sockets per thread. This way you can handle thousand simultaneous connections.

A Worker Thread

When running a lengthy process (SQLing a RBDMS, doing bottlenecked I/O, ...), and to prevent GUI from freezing, encapsulating it in a thread (JobThread), and sending back data when the thread is done (or simulating a process end).