473,467 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why does threading lock in different classes?

2 New Member
Ok first I must preface this question with a disclaimer, I'm really new to threading so this may be a 'newbie' question but I searched google and couldn't find an answer. As I understand it a critical section is code that can only be accessed by one thread, the danger being one thread will overwrite a value before the other is finished and vice versa. What can you do about changes made outside of your class for example, I have a line monitoring program:

Expand|Select|Wrap|Line Numbers
  1. int currentNumber = provider.GetCurrentNumber();
  2. if(provider.CanPassNumber(false, currentNumber))
  3. {
  4. currentNumber+=2;
  5. provider.SetNumber(currentNumber);
  6. }
  7.  
and on another thread I have something like this:

Expand|Select|Wrap|Line Numbers
  1. if(condition)
  2.     provider.SetNumber(numberToSet);
  3.  
Now I'm afraid that in the first function I get currentNumber which is 5, right after that on another thread the number is set to 7 and then it rewrites the 7 to 6, ignoring the change made by the thread that set it to 7.

Is there anyway to lock provider.SetNumber until the first function finishes? The critical section is basically the currentNumber which can be changed by many places in the program.

I hope I made myself clear, if not let me know and I will try to explain myself better.

Also I made the functions really short for the example. In reality the function is much longer and makes changes to currentNumber many times so I don't really want to put a lock around the entire function. If I lock every call to provider.SetNumber and release it after I finish it can change during the time it is released before I lock it again to call provider.SetNumber. Honestly I'm also worried about locking the entire function because of performance and deadlock.
Jan 27 '11 #1
2 1630
bentleyw
32 New Member
One common way to handle this type of situation would be to create a synchronizing variable and use the lock construct to hold until it's free. Here's a nice example of how to do this:

http://www.albahari.com/threading/part2.aspx#_Locking
Jan 29 '11 #2
Aimee Bailey
197 Recognized Expert New Member
Im sure this maybe seen as ugly, but you could try...

Expand|Select|Wrap|Line Numbers
  1. private bool locked = false;
  2. private string _LockableValue = "";
  3.  
  4. public string LockableValue 
  5. {
  6.     set {
  7.         if (!locked) {
  8.             locked = true; 
  9.             _LockableValue = value;
  10.             locked = false;
  11.         }
  12.     }
  13.     get { return _LockableValue; }
  14. }
  15.  
lol.
Feb 5 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Greg Merideth | last post by:
Using Visual C# I created two forms such as namespace test { public class SystemTray : System.Windows.Forms.Form { public createwindow() { stuff here; } public fadewindow() { stuff to fade...
1
by: Jeroen Ceuppens | last post by:
Hi, I want to to include the same function in 2 different classes. class FormView.cs and FormLoad.cs, 2 of Windows.Forms these 2 classes have a couple of the same methods, what is the best way to...
3
by: Mungo Jerrie | last post by:
Hi there :-) I have some problems with the same instance of my xmldocument across 3 different classes. See code below: class one { protected static XmlDocument doc = new XmlDocument(); ...
4
by: MikeAth via DotNetMonster.com | last post by:
Hi all, I have two different classes and one instance of the Form. I would like to access the members of the Form instance in both classes without having to create a new instance in the other...
7
by: Ron | last post by:
Hello, I have 4 classes that use 4 DTS packages on 4 different tables. So I have Dim cls1 As New clsDTS1, cls2 As New clsDTS2 Dim cls3 As New clsDTS3, cls4 As New clsDTS4 Each class has a...
9
by: PCI | last post by:
A Thread is created on form1 that addresses a sub in another class. When attempting to invoke and sub on the main form I recieve an error about a Window Handler needing to be created first. Is...
2
by: greg.merideth | last post by:
I have a project where we have a windows service that creates a remoting object for an external client application to communicate with using ipc. We've discovered the client is making updates to...
3
by: yanjie111 | last post by:
I have several classes build in one project. Class A.functionA() need call Class B.functionB(parameters...). Do I have to create an instance of B inside the Class A source code? I want the...
6
by: Tom | last post by:
This is probably a really basic question, but I know someone will be able to help me. I have a single object A that contains a hash of objects B. Each object B contains its own TCP client object...
3
by: juro | last post by:
Hi, I have a small problem: I'd like to call static variables from different classes - their names are stored in an array. Example of a class: class MyClass1{ public static $mysql_table =...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
1
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,...
0
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...
0
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...
0
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 ...

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.