5,445,109 members and growing! (14,897 online)
Email Password   helpLost your password?
General Programming » Threads, Processes & IPC » Threading     Intermediate

MethodSynchronizer

By Master DJon

How to synchronize threads within a method or multiple methods in an object.
VB, Windows, .NET, Visual Studio, Dev

Posted: 6 Oct 2006
Updated: 12 Oct 2006
Views: 13,960
Bookmarked: 7 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 0.60 Rating: 1.00 out of 5
4 votes, 100.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5

Introduction

So, here I got again into another problem, and no search results gave me the answer I was looking for. The software in development was mostly using only one thread to run, but sometimes it could happen that concurrent threads start to run to update some parts of the software (this is a real-time software). That's where I discovered that some methods weren't thread safe. So what could be my solution? I decided to build an object which could be used to synchronize threads within one or more methods of another object (and possibly multiple objects). This object should respond to these criteria:

  • Is able to support enough concurrent threads (more than 50)
  • Is deployable at least within a singleton object so that all calls made to specific methods are synchronized
  • The entry order doesn't have to respect the exit order necessarily
  • Is able to make threads wait as long as needed.

Thus was born the MethodSynchronizer!!

Usability

This sample is to test the capacity of the MethodSynchronizer.

The MethodSynchronizer is used to synchronize certain moments in your code where multiple threads could run at the same the time. You have to use one object per synchronization grouping. I mean there that, if for example you have an object which is used to read and write to the DB, then you can use one MethodSynchronizer and call, at the beginning of each DBObject methods, InternalThreadSync method to synch all DB calls. Each end of DBOject method has to have a ThreadIsEnded call to let the flow continue. Otherwise, your program will crash. So, ensure that you have a ThreadIsEnded call each time you use InternalThreadSync for all your code flow possibilities.

This object is really easy to use. The only thing you have to care of is where you declare your MethodSynchronizer object. Because this object isn't a Singleton (search this site for Singleton pattern if needed), you have to ensure that the methods using the MethodSynchronizer use the same MethodSynchronizer. So, if you use this within a singleton object, there is no problem; otherwise, you could declare it at a higher level of your program and make a reference to the object using it.

More

I'm been using this sample to get up to 948 concurrent threads processed one after the other. After that, an OutOfMemoryException is thrown by the program.

The reason why I didn't create this object as a Singleton is because I wanted to have the possibility to lock different parts of my software at the same time but not together. So one is independent of the other.

This object should be used when you have to lock for something that could take time. This is really useful when you get external access to your software, which could be anything like DB access, file, webpage, network,... and you only want ONE thread at a time to execute this code.

I hope this could be more useful! If you have questions or comments, send them to thecodeproject@cints.net, or use the forum below.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Master DJon



Occupation: Web Developer
Location: United States United States

Other popular Threads, Processes & IPC articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralWhy roll your ownmemberArchAngel1236:23 10 Oct '06  
AnswerRe: Why roll your ownmemberMaster DJon7:45 10 Oct '06  
GeneralRe: Why roll your ownmemberArchAngel1239:42 10 Oct '06  
GeneralRe: Why roll your ownmemberMaster DJon15:43 10 Oct '06  
GeneralRe: Why roll your ownmemberFilip Duyck0:51 13 Oct '06  
GeneralRe: Why roll your own - Added somememberMaster DJon15:46 10 Oct '06  
GeneralI didn't get the problemmemberMaxGuernsey18:57 9 Oct '06  
GeneralRe: I didn't get the problemmemberMaster DJon7:47 10 Oct '06  
GeneralRe: I didn't get the problemmemberMaxGuernsey18:43 10 Oct '06  
GeneralRe: I didn't get the problemmemberMaster DJon2:43 11 Oct '06  
GeneralRe: I didn't get the problemmemberMaxGuernsey7:57 11 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Oct 2006
Editor: Smitha Vijayan
Copyright 2006 by Master DJon
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project