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

question about Monitor in c++.net


I have a VC++.net code. Two threads are created, one thread keeps
appending elements to the queue, another keeps deleting an element from
the queue.
I tried to use monitor, but it seems that the there is problem. in the
thread of the deleting, seems that the deleting function entered twice
and both
try to delete the queue. The first thread find it is not empty, and try
to delete an element, but before this thread actually delete it,
another thread already
delete the last element. It results in an exception. My code is
attached below.

Can anyone tell me what is wrong with my code?
Thanks in advance

// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Threading;

// Class definition
public __gc class MonitorSample {
public:

MonitorSample();
void AddElement(Object* qValue);
void DeleteElement();
void PrintAllElements();

static void AppendFunction();
static void DeleteFunction();
static void exp();

static MonitorSample *m_sample;

private:

Queue* m_inputQueue;
};

//Define the queue to safe thread access.
MonitorSample::MonitorSample()
{
m_inputQueue = new Queue();
}
//Add an element to the queue and obtain the monitor lock for the queue
object.
void MonitorSample::AddElement(Object* qValue)
{
//Lock the queue.
if(!Monitor::TryEnter(m_inputQueue,100)) return ;
if(m_inputQueue->Count==10)
{
Console::WriteLine("buffer full\n");
}
else m_inputQueue->Enqueue(qValue);
//Unlock the queue.
Monitor::Exit(m_inputQueue);
}
//Delete all elements that equal the given object and obtain the
monitor lock for the queue object.
void MonitorSample::DeleteElement()
{
//Lock the queue.
if(!Monitor::TryEnter(m_inputQueue,100)) return ;
int counter = m_inputQueue->Count;
while(counter > 0)
{
Object* elm = m_inputQueue->Dequeue();
}
//Unlock the queue.
Monitor::Exit(m_inputQueue);
}

//Print all queue elements.
void MonitorSample::PrintAllElements()
{
//Lock the queue.
if(!Monitor::TryEnter(m_inputQueue,100)) return ;

Console::WriteLine("all elements I have:\n");

IEnumerator* elmEnum = m_inputQueue->GetEnumerator();
while(elmEnum->MoveNext())
{
//Print the next element.
Console::WriteLine(elmEnum->Current->ToString());
}
//Unlock the queue.
Monitor::Exit(m_inputQueue);
}
void MonitorSample::AppendFunction()
{
Random *a = new Random();
for(;;)
{
Console::WriteLine("let me append");
// Monitor::Enter(m_sample);
m_sample->AddElement((__box(a->Next(100))));
m_sample->PrintAllElements();

// Monitor::Exit(m_sample);
//Thread::Sleep(a->Next(100,1000));
}
}
void MonitorSample::DeleteFunction()
{
Random *a = new Random();
for(;;)
{
Console::WriteLine("let me delete");

// Monitor::Enter(m_sample);

m_sample->DeleteElement();
m_sample->PrintAllElements();

// Monitor::Exit(m_sample);
//Thread::Sleep(a->Next(100,1000));
}
}

void MonitorSample::exp()
{

Thread * appenthread = new Thread(new
ThreadStart(NULL,AppendFunction));
appenthread->Start();

Thread * deletethread = new Thread(new
ThreadStart(NULL,DeleteFunction));
deletethread->Start();

Thread::Sleep(100000);

deletethread->Abort();
appenthread->Abort();

}

//start multiple thread to do the
void main() {

MonitorSample::m_sample = new MonitorSample();

MonitorSample::exp();
}

Feb 19 '06 #1
2 1670
ww*******@gmail.com wrote:

//Delete all elements that equal the given object and obtain the
monitor lock for the queue object.
void MonitorSample::DeleteElement()
{
//Lock the queue.
if(!Monitor::TryEnter(m_inputQueue,100)) return ;
int counter = m_inputQueue->Count;
while(counter > 0)
{
Object* elm = m_inputQueue->Dequeue();
}
//Unlock the queue.
Monitor::Exit(m_inputQueue);
} How do you expect to get out of the while loop here?

Another point : All your calls to Monitor::Exit should be in finally blocks,
in order to be sure the lock is released even in case of exception.

Arnaud
MVP - VC


//Print all queue elements.
void MonitorSample::PrintAllElements()
{
//Lock the queue.
if(!Monitor::TryEnter(m_inputQueue,100)) return ;

Console::WriteLine("all elements I have:\n");

IEnumerator* elmEnum = m_inputQueue->GetEnumerator();
while(elmEnum->MoveNext())
{
//Print the next element.
Console::WriteLine(elmEnum->Current->ToString());
}
//Unlock the queue.
Monitor::Exit(m_inputQueue);
}
void MonitorSample::AppendFunction()
{
Random *a = new Random();
for(;;)
{
Console::WriteLine("let me append");
// Monitor::Enter(m_sample);
m_sample->AddElement((__box(a->Next(100))));
m_sample->PrintAllElements();

// Monitor::Exit(m_sample);
//Thread::Sleep(a->Next(100,1000));
}
}
void MonitorSample::DeleteFunction()
{
Random *a = new Random();
for(;;)
{
Console::WriteLine("let me delete");

// Monitor::Enter(m_sample);

m_sample->DeleteElement();
m_sample->PrintAllElements();

// Monitor::Exit(m_sample);
//Thread::Sleep(a->Next(100,1000));
}
}

void MonitorSample::exp()
{

Thread * appenthread = new Thread(new
ThreadStart(NULL,AppendFunction));
appenthread->Start();

Thread * deletethread = new Thread(new
ThreadStart(NULL,DeleteFunction));
deletethread->Start();

Thread::Sleep(100000);

deletethread->Abort();
appenthread->Abort();

}

//start multiple thread to do the
void main() {

MonitorSample::m_sample = new MonitorSample();

MonitorSample::exp();
}

Feb 19 '06 #2
You are right, the while should be if!

Thanks for point it out.

Feb 19 '06 #3

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

Similar topics

38
by: Shaun McKinnon | last post by:
HI...Here's my problem...I have a popup window that loads when i want it to, but it's not sized properly. I've set the size, but it doesn't seem to work. I've been on 8 different websites to find...
4
by: Dthmtlgod | last post by:
Could someone please look at my code below, I am trying to nest a couple of if statements together and it is not working. I think I am close. if len(rsMove("MonitorID1")) > 0 and...
6
by: Ramachandran Subramanian | last post by:
I have a question regarding the db2 reset monitor. When I issue the command and do a get snapshot I see most of the counters are reset . How ever the Dynamic SQL snapshot section doesnt seem...
2
by: Jack David | last post by:
Using the code below I am able to monitor a single directory for a new file and then kick-off a process to deal with the file. The question is??? How would I modify this code to be able to monitor...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
17
by: Rainer Queck | last post by:
Hi NG, one more question about thread safety of generic lists. Let's assume a generic list: List<MyTyp> aList = new List<MyType>(); Would it be a problem if one thread removes elements from...
4
by: pcnerd | last post by:
I originally asked this question in the "classic" VB forum. It occured to me after I had sent it that I sent it to the wrong forum. Anyway! Here's the situation. I have VB.NET 2005 Express...
2
by: DeveloperX | last post by:
Hi, Below is a hacked up version of an example I found on MSDN. My only change was to add a third thread and some Sleeps. That's where my question(s) comes in. In the original it seems to work...
2
by: =?Utf-8?B?QnJhdmVzQ2hhcm0=?= | last post by:
I am trying to convert a class I have to generics and I can't seem to find any possible why to implement it. I'm beginning to think I'm doing something I shouldn't or I hit generics limitation. ...
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: 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
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
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
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.