Click here to Skip to main content
15,919,500 members
Articles / Programming Languages / Visual Basic
Article

MethodSynchronizer

Rate me:
Please Sign up or sign in to vote.
1.00/5 (4 votes)
12 Oct 2006CPOL3 min read 36.5K   91   11   11
How to synchronize threads within a method or multiple methods in an object.

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWhy roll your own Pin
ArchAngel12310-Oct-06 5:23
ArchAngel12310-Oct-06 5:23 
AnswerRe: Why roll your own Pin
Master DJon10-Oct-06 6:45
Master DJon10-Oct-06 6:45 
GeneralRe: Why roll your own Pin
ArchAngel12310-Oct-06 8:42
ArchAngel12310-Oct-06 8:42 
GeneralRe: Why roll your own Pin
Master DJon10-Oct-06 14:43
Master DJon10-Oct-06 14:43 
You are probably right on this one. But you know, by my experience, I always get problems when it comes to touchy things and I use microsoft things. Anyhow, even though it could maybe solve my problem, I coudn't personalize it as I am able to do with this object.

I'm one of these programmers which like his own thing. I'm sure you're aware of this behavior on programmers.

Thanks for your reply & comments
GeneralRe: Why roll your own Pin
Filip Duyck12-Oct-06 23:51
Filip Duyck12-Oct-06 23:51 
GeneralRe: Why roll your own - Added some Pin
Master DJon10-Oct-06 14:46
Master DJon10-Oct-06 14:46 
GeneralI didn't get the problem Pin
MaxGuernsey9-Oct-06 17:57
MaxGuernsey9-Oct-06 17:57 
GeneralRe: I didn't get the problem Pin
Master DJon10-Oct-06 6:47
Master DJon10-Oct-06 6:47 
GeneralRe: I didn't get the problem Pin
MaxGuernsey10-Oct-06 17:43
MaxGuernsey10-Oct-06 17:43 
GeneralRe: I didn't get the problem Pin
Master DJon11-Oct-06 1:43
Master DJon11-Oct-06 1:43 
GeneralRe: I didn't get the problem Pin
MaxGuernsey11-Oct-06 6:57
MaxGuernsey11-Oct-06 6:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.