473,809 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

locking question

Hi,

I have a MDIChild with static properties that can be updated at any time
by the MDIParent. These
properties may also at unknown times be accessed by one or more worker
threads.

Now, I'm not too familiar with threads, but is there a danger that these
properties can get corrupted? MDIParent will only SET the properties while
the worker threads will only GET them. If so how do you lock them and to
what?

Morten
Nov 16 '05 #1
3 1346
If you don't use any kind of syncronization, there's a possibility that the
worker threads will read the properties before they've been updated.
Generally speaking, when two or more threads need access to a resource,
access to that resource should be syncronized. All you need to do is lock
on a syncronization object in the get/set of your MDIChild class.
Something like this (note that I used static becasue you mentioned in your
post that your properties are static):

// sync object defined in MDIChild class
private static object syncObject = new object();
...
// the static property of the MDIChild class
private static int theInt = 0;
...
// the static property accessors of MDIChild
public static int MyIntProperty
{
get
{
lock (syncObject)
{
return theInt;
}
}
set
{
lock (syncObject)
{
theInt = value;
}
}
}

Check out this link for more info on multithreaded design:
http://msdn.microsoft.com/library/de...us/dv_vstechar
t/html/vbtchasyncprocv b.asp

hth

-Joel
--------------------
Subject: locking question
From: Morten Wennevik <Mo************ @hotmail.com>
Content-Type: text/plain; format=flowed; charset=iso-8859-15
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Date: Mon, 19 Apr 2004 22:38:20 +0200
Message-ID: <opr6p0x6fxhntk fz@localhost>
User-Agent: Opera7.23/Win32 M2 build 3227
Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
NNTP-Posting-Host: h192n1fls32o817 .telia.com 213.67.67.192
Lines: 1
Path: cpmsftngxa10.ph x.gbl!TK2MSFTFE ED01.phx.gbl!TK 2MSFTNGP08.phx. gbl!TK2MSFTNGP1 2
..phx.gblXref: cpmsftngxa10.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:2383 00
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp

Hi,

I have a MDIChild with static properties that can be updated at any time
by the MDIParent. These
properties may also at unknown times be accessed by one or more worker
threads.

Now, I'm not too familiar with threads, but is there a danger that these
properties can get corrupted? MDIParent will only SET the properties while
the worker threads will only GET them. If so how do you lock them and to
what?

Morten

--------------------------------------------------------------------
This reply is provided AS IS, without warranty (express or implied).
Nov 16 '05 #2
Ah,

Thank you, I knew of the possibility of access violations, but the lock
required some object.
Since the properties are static I couldn't use 'this'. So, any object
will do?

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
Morten Wennevik <Mo************ @hotmail.com> wrote:
Thank you, I knew of the possibility of access violations, but the lock
required some object.
Since the properties are static I couldn't use 'this'. So, any object
will do?


Yes, but preferably (IMO) one which no other classes have access to. I
usually have a separate variable which contains a reference to an
object which is *solely* used for locking. I may have several in a
class, depending on the locking granularity required.

I'm writing a sort of introduction and tips page on multithreading -
I'll post when I'm done with it. (It may be some time though - it's a
nasty subject!)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

4
4571
by: Michael Chermside | last post by:
Ype writes: > For the namespaces in Jython this 'Python internal thread safety' > is handled by the Java class: > > http://www.jython.org/docs/javadoc/org/python/core/PyStringMap.html > > which has almost all of it public methods Java synchronized: > > http://cvs.sourceforge.net/viewcvs.py/jython/jython/org/python/core/PyStringMap.
2
1815
by: Geoffrey | last post by:
We have developed a python class that can read data files created from another application. These target files are C-ISAM files used for accounting applications so the "primary" application may be reading and/or writing to portions of the files ("records") at the same time. The python class we developed opens the requested file in 'rb' mode, reads the file header (first 512k) and using information supplied in the file header (such as...
0
3238
by: Steve McWilliams | last post by:
Hello, I am relatively new to MySql (4.0.14) but I have read through the relevent documentation and am still confused about how row level locking behaves with InnoDB tables. I created a database with a single innodb table which has 2 columns, one of which is indexed. The locking behavior I see when I test against this database is that it uses row level locks if the "SELECT ... FOR UPDATE" involves the indexed column, but uses table...
16
8935
by: Nid | last post by:
How do I do row-level locking on SQL Server? Thanks, Nid
2
1353
by: spammy | last post by:
hi all, im trying to establish whether i have a race condition or critical section in the following. i have a dataaccess class that continually retireves a table from a sqlserver (which may be slow). it provides a cached copy of this table to clients, for them to read from (note: there are no writes). the idea being if a particular table retrival takes time, the class would still provide a cached copy. now the code:
0
4053
by: Megan | last post by:
Hi- I have a question about the Microsoft Access Record-Locking Information Icon. Whenever I, or my friends, open my MS Access database, the Microsoft Access Record-Locking Information Icon appears. When we close the Access database, the icon disappears. However now all of the sudden, the Microsoft Access Record-Locking
16
3470
by: akantrowitz | last post by:
In csharp, what is the correct locking around reading and writing into a hashtable. Note that the reader is not looping through the keys, simply reading an item out with a specific key: If i have the following hashtable h which has multiple readers and 1 writer (on different threads) is this the correct locking below: lock (h.syncroot) {
6
4095
by: shaanxxx | last post by:
I have global variable which is being shared between threads (problem is not connected with thread). Without using any mutex i have do some operation global variable in *consistent* way. consider following code : int i = 0 fun (int j)
0
1323
by: xpding | last post by:
Hello, I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well. There is another class need to access and manipulate a list of MyEmbededList (please refer to the MyTestClass below). I am not sure whether I implements the right locking mechanism here and hope someone can give me some advices. I have provided some codes for these two classes below. My questions are: 1. Am I...
15
5260
by: Matt Brandt | last post by:
I am trying to get multiple threads to lock specific regions of a file. However, since each thread has the same PID, it appears that a lock by one thread does not block another thread from the same process. How does one maintain file locking between sibling threads? - Matt
0
9600
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
10633
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...
1
10375
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
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
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
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.