473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Scoping and Synchronization

I have an object that needs to have a global scope within my ASP.Net
application. As I can find no way to make this a server component that
has a global scope, I have given it a static constructor for
initializing its inner parts. This is an id generator object for
inserting rows into a particular table (for various reasons the DBA
cannot make this table have a db server generated id). My concern stems
from the calls to the static getNextId() method. How do I do something
similar to Java's synchronized so that only one call happens to this
method at a time to avoid a race condition?

Thanks.

Nov 18 '05 #1
2 873
you could use lock keywork to allow only one thread to access the code at a
given point. the other threads will be placed in a queue.

if the id generation is just an incremental value then you can use interlock
class. it has a method called increment.

--

Regards,

Hermit Dave (D'way)
http://hdave.blogspot.com

(I hear what you're saying.. but lets try it my way first)

"No One" <no***@yahoo.com> wrote in message
news:41***************@yahoo.com...
I have an object that needs to have a global scope within my ASP.Net
application. As I can find no way to make this a server component that
has a global scope, I have given it a static constructor for
initializing its inner parts. This is an id generator object for
inserting rows into a particular table (for various reasons the DBA
cannot make this table have a db server generated id). My concern stems
from the calls to the static getNextId() method. How do I do something
similar to Java's synchronized so that only one call happens to this
method at a time to avoid a race condition?

Thanks.

Nov 18 '05 #2
To No One in Particular...

http://www.geocities.com/jeff_louie/OOP/oop4.htm

A Static Counter

Here is the classic example of a static counter that is zero based. It
contains a
static field "uniqueID" and a thread safe static method "GetUniqueID"
that
increments the unique ID:

/// <summary>
/// Summary description for TestStatic.
/// </summary>
class TestStatic
{
*** // static stuff
*** private static int uniqueID= 0;
*** private static int GetUniqueID()
*** {
******* lock(typeof(TestStatic))
******* {
*********** return uniqueID++; // returns zero at start
******* }
*** }
*** // member stuff
*** private int identity;
*** public TestStatic()
*** {
******* this.identity= TestStatic.GetUniqueID();
*** }
*** public int Identity
*** {
******* get
******* {
*********** return identity;
******* }
*** }
}

public class Test
{
**** /// <summary>
*** /// The main entry point for the application.
*** /// </summary>
**** [STAThread]
*** static void Main(string[] args)
*** {
******* //
******* // TODO: Add code to start application here
******* //
******* TestStatic ts1= new TestStatic();
******* TestStatic ts2= new TestStatic();
******* Console.WriteLine(ts1.Identity.ToString());
******* Console.WriteLine(ts2.Identity.ToString());
******* Console.ReadLine();
*** }
}

Regards,
Jeff
My concern stems from the calls to the static getNextId() method. How

do I
do something similar to Java's synchronized so that only one call
happens to
this method at a time to avoid a race condition?<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3

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

Similar topics

2
by: David Stockwell | last post by:
Hi, Another of my crazy questions. I'm just in the process of learning so bear with me if you can. I actually ran it.... with two test cases TEST CASE 1: Say I have the following defined:...
4
by: Joel Gordon | last post by:
Hi, When I try and compile the a class containing the following method : public void doSomething() { for (int i=0; i<5; i++) { IList list = new ArrayList(); Console.WriteLine( i /...
4
by: scott | last post by:
hi all, Thx to any one that can offer me help, it will be much appreciated. iv got a multithreaded program and need to use thread synchronization. The synchronization does not have to...
0
by: Rod | last post by:
I orginally posted this to microsoft.public.sqlserver.ce but had not received any responses. I have a CF.NET application (C#) with a SqlCE database. We had originally planned to use SQL...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
3
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
17
by: Chad | last post by:
The following question stems from Static vs Dynamic scoping article in wikipedia. http://en.wikipedia.org/wiki/Scope_(programming)#Static_versus_dynamic_scoping Using this sites example, if I...
12
by: emma_middlebrook | last post by:
Hi Say you had N threads doing some jobs (not from a shared queue or anything like that, they each know how to do their own set of jobs in a self-contained way). How can you coordinate them so...
3
by: SPECTACULAR | last post by:
Hi all. I have a question here.. what kind of scoping does C++ use? and what kind does Smalltalk use? I know smalltalk is a little bit old .. but any help would be appreciated.
0
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.