Register for your free account! | Forgot your password?

You last visited: Today at 03:32

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Multi Threading

Discussion on Multi Threading within the CO2 Private Server forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Multi Threading

I know it's not Conquer related like that, but Multi Threading is a very big part, when developing Conquer PServers.

Quote:
Originally Posted by Consider the following guidelines when using multiple threads:
Don't use Thread.Abort to terminate other threads. Calling Abort on another thread is akin to throwing an exception on that thread, without knowing what point that thread has reached in its processing.

Don't use Thread.Suspend and Thread.Resume to synchronize the activities of multiple threads. Do use Mutex, ManualResetEvent, AutoResetEvent, and Monitor.

Don't control the execution of worker threads from your main program (using events, for example). Instead, design your program so that worker threads are responsible for waiting until work is available, executing it, and notifying other parts of your program when finished. If your worker threads do not block, consider using thread pool threads. Monitor.PulseAll is useful in situations where worker threads block.

Don't use types as lock objects. That is, avoid code such as lock(typeof(X)) in C# or SyncLock(GetType(X)) in Visual Basic, or the use of Monitor.Enter with Type objects. For a given type, there is only one instance of System.Type per application domain. If the type you take a lock on is public, code other than your own can take locks on it, leading to deadlocks. For additional issues, see Reliability Best Practices.

Use caution when locking on instances, for example lock(this) in C# or SyncLock(Me) in Visual Basic. If other code in your application, external to the type, takes a lock on the object, deadlocks could occur.

Do ensure that a thread that has entered a monitor always leaves that monitor, even if an exception occurs while the thread is in the monitor. The C# lock statement and the Visual Basic SyncLock statement provide this behavior automatically, employing a finally block to ensure that Monitor.Exit is called. If you cannot ensure that Exit will be called, consider changing your design to use Mutex. A mutex is automatically released when the thread that currently owns it terminates.

Do use multiple threads for tasks that require different resources, and avoid assigning multiple threads to a single resource. For example, any task involving I/O benefits from having its own thread, because that thread will block during I/O operations and thus allow other threads to execute. User input is another resource that benefits from a dedicated thread. On a single-processor computer, a task that involves intensive computation coexists with user input and with tasks that involve I/O, but multiple computation-intensive tasks contend with each other.

Consider using methods of the Interlocked class for simple state changes, instead of using the lock statement (SyncLock in Visual Basic). The lock statement is a good general-purpose tool, but the Interlocked class provides better performance for updates that must be atomic. Internally, it executes a single lock prefix if there is no contention. In code reviews, watch for code like that shown in the following examples. In the first example, a state variable is incremented:
Source:
BaussHacker is offline  
Thanks
2 Users
Old 08/17/2011, 19:01   #2
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,283
Received Thanks: 4,192
I would just use the Async Socket system that I released in Project Kanji and make a multithreaded queue that makes use of Pulsing.
Spirited is offline  
Old 08/17/2011, 19:12   #3
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by Fаng View Post
I would just use the Async Socket system that I released in Project Kanji and make a multithreaded queue that makes use of Pulsing.
And I would use what's recommended.
BaussHacker is offline  
Old 08/18/2011, 05:59   #4
 
GRASSHOPPA's Avatar
 
elite*gold: 0
Join Date: Jun 2008
Posts: 113
Received Thanks: 19
I've been getting pretty interested in threading lately..so of course I need to ask

whats the difference between multi threading and thread pools?
multi threading seems to do the same thing as a thread pool with its queuing and such..yes I'm going to google it later..just curious what people like about one over the other
GRASSHOPPA is offline  
Old 08/18/2011, 12:26   #5


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Threadpools are a form of multi-threading, but in the simplest terms having the program using more than 1 thread is multi-threading instead of a single threaded application design.

But more generally speaking if you have a big 'task' to complete in the application using 1 thread for every logical core that the processor has, and then spreading the load would be the best way to achieve multi-threading.
Korvacs is offline  
Old 08/18/2011, 18:34   #6
 
GRASSHOPPA's Avatar
 
elite*gold: 0
Join Date: Jun 2008
Posts: 113
Received Thanks: 19
that makes a bit more sense
Do threadpools use each logical core?if not what does?
GRASSHOPPA is offline  
Old 08/18/2011, 18:44   #7


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Quote:
Originally Posted by GRASSHOPPA View Post
that makes a bit more sense
Do threadpools use each logical core?if not what does?
No, the default threadpool size for .net is 25, it can use as many as 200 i think the limit is (Google this to confirm), the idea of using the .net threadpool is to create a work task and send it to the threadpool. So its not meant to use as a part of the core application threading.

Everytime you create a thread it just gets dumped onto a pile of threads which are distributed by the kernel i think? Dont know specifics about thread distribution across a processor.
Korvacs is offline  
Reply


Similar Threads Similar Threads
Socket/Threading Question
07/31/2011 - CO2 Programming - 3 Replies
Alright so I've been following InfamousNoone's C# for idiots tutorial (video set) and I've just finished watching them all. I can now say I'm very very interested in sockets and whatnot. So the problem is, I'm trying to use sockets to creating a mini Messaging client and I've gotten to the point where I can send a message back in forth. But this is how it works. The ONLY way you can send a message is if you receive one first. That's obviously a problem! I've tried to start multiple threads...
My Socket Wrapper With Multi Threading and Events Similar to Classic Winsock
10/25/2010 - CO2 Programming - 9 Replies
Hi guys to those who are planning to make their own proxy and want to have a socket wrapper which is similar to classic winsock then this class below is the one you are looking. I created a multi threading socket of System.Net.Sockets. I also created the events similar to classic winsock such as OnConnectionRequest, OnDisconnection, OnConnect, OnDataArrival, OnSendComplete and OnError. Imports System.Net.Sockets Imports System.Net Imports System.Text Imports System.Threading
[C++] Problem with threading
05/17/2009 - CO2 Programming - 2 Replies
Actually the problem is more like with passing a function as pointer to the CreateThread or _beginthreadex, BOOL Connect() { if(m_Enabled != TRUE) return FALSE; sockaddr_in addr; addr.sin_family = AF_INET;
[C#] Cross-Threading ?
10/14/2008 - .NET Languages - 7 Replies
Hello, Is there a flag or whatever that lets me use crossthreading freely? Without the help of a Workerthread. Whenever I try to change a variable it doesn't let me because Crossthreading isn't safe (eg. accessing the variables from other threads), but It's also hard to find a solution for this. I'm starting another thread that contains a function to download the source code of another page and process the wanted information into variables, however I cannot do that with a thread. The only...



All times are GMT +1. The time now is 03:35.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.