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

How to make a function thread safe in mc++?

Greetings,

I was looking at the C#'s "lock()" statement, and I was wondering if there
was an equivalent or recommended way of doing this in MC++.

I have a thread which I am calling a function from mc++, and I was wondering
what
the recommended ways of making it thread safe within the mc++ world.

Thanks in advance for any suggestions.
Nov 17 '05 #1
7 1178
Right after I made the post I stumbled accross something that I could use for
thread safety, so I thought I would post in case someone else needs it for a
solution.

Solution:
void Foo()
{
// At the beginning of your function.
System::Threading::Monitor::Enter(this);

.... Do some code work

// Add the end of your function (this has to be called)
System::Threading::Monitor::Exit(this);
}

"BartMan" wrote:
Greetings,

I was looking at the C#'s "lock()" statement, and I was wondering if there
was an equivalent or recommended way of doing this in MC++.

I have a thread which I am calling a function from mc++, and I was wondering
what
the recommended ways of making it thread safe within the mc++ world.

Thanks in advance for any suggestions.

Nov 17 '05 #2
BartMan wrote:
Right after I made the post I stumbled accross something that I could
use for thread safety, so I thought I would post in case someone else
needs it for a solution.

Solution:
void Foo()
{
// At the beginning of your function.
System::Threading::Monitor::Enter(this);

... Do some code work

// Add the end of your function (this has to be called)
System::Threading::Monitor::Exit(this);
}


That's precisely what the C# 'lock' construct does.

-cd
Nov 17 '05 #3
I think it would be pretty easy to wrap this into a RAII __nogc class
with a little help from gcroot.

Nov 17 '05 #4
Hello Nemanja,

Thanks for the great idea, I didn't know about gcroot which is really
usefull. :)
Here is my attempt.

public __gc class lock
{
public:
lock(System::Object* obj)
{
threadObject = obj;
System::Threading::Monitor::Enter(threadObject);
}
~lock()
{
System::Threading::Monitor::Exit(threadObject);
}
protected:
gcroot<System::Object*> threadObject;
};
"Nemanja Trifunovic" wrote:
I think it would be pretty easy to wrap this into a RAII __nogc class
with a little help from gcroot.

Nov 17 '05 #5
I hope you mean __nogc lock.

__gc classes don't have deterministic destructors. Take a look at an
article I wrote a while ago on the topic:

http://www.codeproject.com/managedcpp/managedraii.asp

Nov 17 '05 #6
Oops, you are right.
Very good article, thanks for the post!

"Nemanja Trifunovic" wrote:
I hope you mean __nogc lock.

__gc classes don't have deterministic destructors. Take a look at an
article I wrote a while ago on the topic:

http://www.codeproject.com/managedcpp/managedraii.asp

Nov 17 '05 #7
Hi Carl!
Right after I made the post I stumbled accross something that I could
use for thread safety, so I thought I would post in case someone else
needs it for a solution.

Solution:
void Foo()
{
// At the beginning of your function.
System::Threading::Monitor::Enter(this);

... Do some code work

// Add the end of your function (this has to be called)
System::Threading::Monitor::Exit(this);
}


That's precisely what the C# 'lock' construct does.


Almost... the C# also does an try-finally construct..

System::Threading::Monitor::Enter(this);
__try
{
// Do some code work
}
__finally
{
System::Threading::Monitor::Exit(this);
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #8

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

Similar topics

7
by: lawrence | last post by:
2 Questions: 1.) Can anyone think of a way to speed up this function? It is terribly slow. I plan to reduce the number of directories to 3, which I guess will speed it up in the end. 2.) This...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
2
by: Aaron Cutlip | last post by:
I have been looking all over and have seen many possible ways to create a Syncronized Shared Function in VB.NET, but I would like some advice that will make my life easier. Given the following...
6
by: Samuel R. Neff | last post by:
I'm having weird results with a form that is already displayed modally (via ShowDialog) displaying a second form via ShowDialog. The last form is not modal even though it's called with ShowDialog....
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
8
by: ecir.hana | last post by:
Dear list, maybe I'm overlooking something obvious or this is not possible at all or I don't know. Please, consider the following code: ## insert here anything you like def changer():
10
by: _mario.lat | last post by:
hallo, what does it means "the function is not thread-safe"? thak you in advance, Mario.
0
saputello
by: saputello | last post by:
hello, hello, hello can anyone see the problem with this drive() function? i can't get it to work! class Car { // properties public var mc:MovieClip; private var acceleration:Number;...
44
by: climber.cui | last post by:
Hi all, Does anyone have experience on the thread-safty issue with malloc()? Some people said this function provided in stdlib.h is not thread- safe, but someone said it is thread safe. Is it...
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...
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...
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
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
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...

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.