473,386 Members | 1,796 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,386 software developers and data experts.

problem with Threading.Monitor

ste
this code not work: it is no possible Exit from Monitor
Why ?

thank
ste

////////

using System;
using System.Collections;
using System.Threading;

namespace consoleProtection
{
class Class1
{
ArrayList _obj = new ArrayList();

[STAThread]
static void Main(string[] args)
{
Class1 newClass1 = new Class1();
newClass1.go();
Sleep(15000);
}

public void go()
{
Thread _th1 = new Thread(new ThreadStart(this.enter));
_th1.Start();
Thread.Sleep(100);
Thread _th2 = new Thread(new ThreadStart(this.exit));
_th2.Start();
Thread.Sleep(100);
Thread _th3 = new Thread(new ThreadStart(this.enter));
_th3.Start();
}

void enter()
{
Console.WriteLine("try to enter");
Monitor.Enter(_obj);
Console.WriteLine("enter");
}

void exit()
{
Monitor.Exit(_obj);
Console.WriteLine("exit");
}
}
}

Nov 15 '05 #1
5 1668

"ste" <st*********@libero.it> wrote in message
news:PH***********************@twister2.libero.it. ..
this code not work: it is no possible Exit from Monitor
Why ?


Let's recap:

You are spawing another thread, which enters the arraylist monitor, and then
dies.
Then another thread that exits the monitor (which it never entered), and
then dies.
Then another thread that tries to enter the monitor, and if sucessful, will
die.

I don't really know what happens when you let a thread die when it has an
object locked. It should never come up.

You don't call the threading lock mechanisms using background threads. What
are you trying to acomplish?

David
Nov 15 '05 #2
The calls to Enter and Exit must occur on the same thread. When you
call this.enter on the first thread, it holds a lock on that object
indefinitely (I believe, I don't know if the lock is released when the
thread dies, I could be wrong). When you try and exit on the second thread,
it won't work because you never entered into it on that thread.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

"ste" <st*********@libero.it> wrote in message
news:PH***********************@twister2.libero.it. ..
this code not work: it is no possible Exit from Monitor
Why ?

thank
ste

////////

using System;
using System.Collections;
using System.Threading;

namespace consoleProtection
{
class Class1
{
ArrayList _obj = new ArrayList();

[STAThread]
static void Main(string[] args)
{
Class1 newClass1 = new Class1();
newClass1.go();
Sleep(15000);
}

public void go()
{
Thread _th1 = new Thread(new ThreadStart(this.enter));
_th1.Start();
Thread.Sleep(100);
Thread _th2 = new Thread(new ThreadStart(this.exit));
_th2.Start();
Thread.Sleep(100);
Thread _th3 = new Thread(new ThreadStart(this.enter));
_th3.Start();
}

void enter()
{
Console.WriteLine("try to enter");
Monitor.Enter(_obj);
Console.WriteLine("enter");
}

void exit()
{
Monitor.Exit(_obj);
Console.WriteLine("exit");
}
}
}

Nov 15 '05 #3
ste wrote:
this code not work: it is no possible Exit from Monitor
Why ?


As was pointed out, what you're explicitly trying to do doesn't work.

It's not clear to me if you're just trying to understand how Monitor works
or if you really need to use a mechanism that supports this, but if you do
need it, use a Mutex instead:

using System;
using System.Collections;
using System.Threading;

namespace consoleProtection
{
class Class1
{
static Mutex mtx = new Mutex();
ArrayList _obj = new ArrayList();

[STAThread]
static void Main(string[] args)
{
Class1 newClass1 = new Class1();
newClass1.go();
Thread.Sleep(15000);
}

public void go()
{
Thread _th1 = new Thread(new ThreadStart(this.enter));
_th1.Start();
Thread.Sleep(100);
Thread _th2 = new Thread(new ThreadStart(this.exit));
_th2.Start();
Thread.Sleep(100);
Thread _th3 = new Thread(new ThreadStart(this.enter));
_th3.Start();
}

void enter()
{
Console.WriteLine("try to enter");
// if someone already has locked this, it will block
mtx.WaitOne();
Console.WriteLine("enter");
}

void exit()
{
// works cross threads
mtx.ReleaseMutex();
Console.WriteLine("exit");
}
}
}
--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
Nov 15 '05 #4
ste
first: thank for the answer ti you and to the other

second: my problem is a little more complex and i have post a "simple
version" to show the core of the question: you say me that it's no correct
that a thread die without release the monitor. But this is in the MSDN
documentation ?

In my program a thread lock a resource and another thread free. It's correct
to use a Mutex ?

thank
ste

David Browne wrote:

"ste" <st*********@libero.it> wrote in message
news:PH***********************@twister2.libero.it. ..
this code not work: it is no possible Exit from Monitor
Why ?


Let's recap:

You are spawing another thread, which enters the arraylist monitor, and
then dies.
Then another thread that exits the monitor (which it never entered), and
then dies.
Then another thread that tries to enter the monitor, and if sucessful,
will die.

I don't really know what happens when you let a thread die when it has an
object locked. It should never come up.

You don't call the threading lock mechanisms using background threads.
What are you trying to acomplish?

David


Nov 15 '05 #5
The thread that acquires a mutex must be the thread that releases it. If the
thread that originally acquired the mutex terminates normally while holding
ownership the mutex is released, the state is set to signalled, and the next
waiting thread is signalled. If the thread that owns the mutex never
releases it and does not terminate then no other thread will be able to
acquire it.

This is all clearly documented. If this behavior is not suitable then you
will need to invent your own mechanism.

Mutexes are suitable when synchronizing between managed and unmanaged code,
or between processes. Monitors are typically recommended when synchronizing
threads within the same managed process.

"ste" <st*********@libero.it> wrote in message
news:gA***********************@twister2.libero.it. ..
first: thank for the answer ti you and to the other

second: my problem is a little more complex and i have post a "simple
version" to show the core of the question: you say me that it's no correct
that a thread die without release the monitor. But this is in the MSDN
documentation ?

In my program a thread lock a resource and another thread free. It's correct to use a Mutex ?

thank
ste

David Browne wrote:

"ste" <st*********@libero.it> wrote in message
news:PH***********************@twister2.libero.it. ..
this code not work: it is no possible Exit from Monitor
Why ?


Let's recap:

You are spawing another thread, which enters the arraylist monitor, and
then dies.
Then another thread that exits the monitor (which it never entered), and
then dies.
Then another thread that tries to enter the monitor, and if sucessful,
will die.

I don't really know what happens when you let a thread die when it has an object locked. It should never come up.

You don't call the threading lock mechanisms using background threads.
What are you trying to acomplish?

David

Nov 15 '05 #6

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

Similar topics

1
by: Peter Schmitz | last post by:
Hi, I just tried to convert my current project to the new beta 2 of the .net framework and I encountered some problems with my thread synchronization - that formerly worked well. When I...
5
by: Leonardo Hyppolito | last post by:
Hello! I am trying to implement a program that uses Threads. I chose the producers and consumers scenario. The producers put a "product" (which is an int number) in a shared storage place of one...
2
by: Gordon Knote | last post by:
Hi I'm currently writing a VB.NET service that uses external Dlls, like this: Private Declare Function ReadSettings Lib "mydll.dll" () Now, how can I protect this function (ReadSettings)...
4
by: Bob | last post by:
- For cleanup, is it sufficient to set a Thread to Nothing after it's done? - It is OK to pass objects out of the thread? (dumb question maybe but I want to be sure) - What's the best way to...
8
by: Michael Kennedy | last post by:
Hi, I have been looking into the claim that the keyword lock is not safe when exceptions are possible. That lead me to try the following code, which I think has uncovered a serious error in the...
11
by: # Cyrille37 # | last post by:
Hello all, I come to you to getting help for managing multi threading and database connection. My project use Xml-Rpc to receive messages, so each call come from a different thread. Incoming...
0
by: fiefie.niles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message while...
0
by: Jovo Mirkovic | last post by:
Hi, Any Windows action which minimizes all windows (win + D, ctrl + alt + del) is causing program error I have two forms: Form1 is shown on primary monitor on dual head graphics display
20
by: cty0000 | last post by:
I have some question.. This is my first time to use thread.. Following code does not have error but two warring The warring is Warning 2 'System.Threading.Thread.Suspend()' is obsolete:...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...
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...

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.