473,842 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multi-thread question with Singleton

Sorry this is so long winded, but here goes. Following the model of

http://msdn2.microsoft.com/en-us/lib...pcchannel.aspx
I made a remote object using the IpcChannel Class (vs 2005, vb, fw 2.0).
Everyting works fine. The object is registered with
WellKnownObject Mode.Singleton
The remote object appears at the bottom of this posting. The code is
deliberately obtuse to expose an issue about when and how single and multiple
threading happen. With a 2 second delay via threading.sleep (blocking) or a
coded while loop (non-blocking), and with clients calling OneUp in rapid
succession, the behavior I see is as follows:

1. With the 2 second delay via while loop (ie threading.sleep is commented
out), multiple client calls to OneUp stack up, and every 2 seconds a call is
completed. In other words, it looks like .net/windows single threads the
calls to OneUp. There is no reentrancy.

2. With the 2 second delay via threading.sleep (ie while loop is commented
out), ONE client application behaves like #1 above.

3. With the 2 second delay via threading.sleep (ie while loop is commented
out), MULTIPLE client applications get tangled up. While the sleep of the
first call to OneUp is running, the second call is allowed to happen, and
thus the functionality of OneUp fails. If the two clients invoke OneUp at
least 2 seconds apart, all is well. If they run in rapid succession, they
both return the same value, ie they run multi-threaded because OneUp is
re-entered. To make things work right, I would have to apply some form of
thread coordination, eg Interlocked.Inc rement would suffice in this case.

So, when the singleton remote object does a Sleep, its behavior is different
than if it does not do a Sleep. Or am I fooling myself in this observation?
The Sleep is one kind of thread blocking - will the behavior change if a
different kind of blocking happens? How about a WebRequest? How can I
guarantee that a sub in a class does not block? Or maybe throws an exception
if it does block?

The reason for these questions is as follows. I would like to avoid coding
multithread coordination in all the methods that will ultimately be in the
singleton remote object. I know how to do it if I have to, but I don't want
to. What I want my singleton remote object to do is be single threaded and
non-reentrant. If one client is using a remote function, I want others to
block. Can it be done?

The remote object is as follows:

Public Class SingletonRemote Object
Inherits MarshalByRefObj ect

Private iOneUp As Integer

Public Function OneUp() As Integer
Dim k As Integer = iOneUp + 1
Dim dt As DateTime = Now.AddSeconds( 2)

'While Now < dt : End While ' non-blocking 2 second delay
System.Threadin g.Thread.Sleep( 2000) ' blocking 2 second delay

iOneUp = k
Return k
End Function

End Class

Jun 27 '08 #1
1 1714
AMercer <AM*****@discus sions.microsoft .comwrote:
Sorry this is so long winded, but here goes. Following the model of

http://msdn2.microsoft.com/en-us/lib...pcchannel.aspx
I made a remote object using the IpcChannel Class (vs 2005, vb, fw 2.0).
Everyting works fine. The object is registered with
WellKnownObject Mode.Singleton
The remote object appears at the bottom of this posting. The code is
deliberately obtuse to expose an issue about when and how single and multiple
threading happen. With a 2 second delay via threading.sleep (blocking) or a
coded while loop (non-blocking), and with clients calling OneUp in rapid
succession, the behavior I see is as follows:

1. With the 2 second delay via while loop (ie threading.sleep is commented
out), multiple client calls to OneUp stack up, and every 2 seconds a call is
completed. In other words, it looks like .net/windows single threads the
calls to OneUp. There is no reentrancy.
Well, at that point you've got a tight loop - so it's not like you're
playing nicely and letting other threads have much of a look-in.
2. With the 2 second delay via threading.sleep (ie while loop is commented
out), ONE client application behaves like #1 above.

3. With the 2 second delay via threading.sleep (ie while loop is commented
out), MULTIPLE client applications get tangled up. While the sleep of the
first call to OneUp is running, the second call is allowed to happen, and
thus the functionality of OneUp fails. If the two clients invoke OneUp at
least 2 seconds apart, all is well. If they run in rapid succession, they
both return the same value, ie they run multi-threaded because OneUp is
re-entered. To make things work right, I would have to apply some form of
thread coordination, eg Interlocked.Inc rement would suffice in this case.
Indeed. That's almost certainly the way to go.
So, when the singleton remote object does a Sleep, its behavior is different
than if it does not do a Sleep. Or am I fooling myself in this observation?
The Sleep is one kind of thread blocking - will the behavior change if a
different kind of blocking happens? How about a WebRequest? How can I
guarantee that a sub in a class does not block? Or maybe throws an exception
if it does block?
I suspect you'd find that in *some* cases your tight loop would act in
exactly the same way - particularly on multi-processor boxes.

Both of your ways of pausing are blocking, by the way - they're
blocking the current thread from doing any more work until they've
finished the "pause".
The reason for these questions is as follows. I would like to avoid coding
multithread coordination in all the methods that will ultimately be in the
singleton remote object. I know how to do it if I have to, but I don't want
to. What I want my singleton remote object to do is be single threaded and
non-reentrant. If one client is using a remote function, I want others to
block. Can it be done?
Use a shared lock which you take out for the whole of every remote
call. It's fairly easy to do, although it won't scale at all, of
course.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2

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

Similar topics

1
1815
by: Stephen Thorne | last post by:
Decorators have been getting lots of air-time at the moment, but only really the syntax. After a short discussion on irc the other night I decided to download python2.4 from experimental and write out some truely worthy decorator hacks. I implemented multimethods as a warm-up, and then implemented tail call elimination. Presented here is a brief synopsis of both, and then the implementations....
37
4913
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours, Pujo
1
2029
by: Alex | last post by:
I need to duplicate some of the multi-page tiff and thumbnail viewing functionality found in the Microsoft Office Document Imaging application for an in-house company application. Does anyone know what multi-page tiff controls were used to create the Microsoft Office Document Imaging app or where I can buy similar .Net controls that will allow me to view a multi-page TIFF (zoom in/out, display thumbnails, rotate current frame left/right...
6
2240
by: Adam Hartshorne | last post by:
The input to a function of a 3rd party library I want to use requires a double**, which is a multi-dimension array of doubles. I have looked on the net etc and seen several ways of supposedly doing this, but I don't seem to be able to get them to work. I was wondering if anybody can tell me what I am doing wrong. int rows = 10 ; int cols = 10 ;
6
4902
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on a table called COURSES. This table has 2 fields CATEGORY_ID, COURSE_NAME. The CATEGORY_ID is a FK in COURSES and a PK in CATEGORIES. I want to populate the course list box based on any category(s)
3
3299
by: kathy | last post by:
I have a general question. If I write a application using any language(C/C++/C#/VB). How to make sure it only has one instance running? What I need to write in source code? If multi-instances run. Is any data confilct exsisted? Assuming I put the .exe in shared network drive and the multi-instances run on different PC. How about the multi-instances run on same PC?
6
11784
by: CindyH | last post by:
Hi Does anyone know how to create a multi column combo box from the same table? Thanks, Cindy
3
1563
by: D. Patrick | last post by:
I need to guide the training for some good ASP.NET programmers who don't have Windows Forms experience, and they will soon be put onto a project for multi-threaded winforms development. Can anyone suggest a good book for multi-threaded winforms learning? Or even some good URLs?
1
9329
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part...
3
2095
by: Fred | last post by:
We all agree that multi-core is where the industry is headed. Can the .NET framework take advantage of multi-cores? are the programs written in .NET multi-threaded by default or is additional tweaking required? Will there be any new features in the new version of the .NET framework to extend multi-core capabilities? Fred
0
9872
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9715
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10945
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10612
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10672
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9453
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7858
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4499
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.