C++ sytnax for thread locking

B

bill

All,

I have a WindowsForm app and am happily getting data from my DLL which
is on a different thread than my UI. I have now arrived at the
situation where I need to lock the data that is being communicated.
Unhappily, again, I can find no examples using C++ to lock threads in
a .NET Windows Form application. All of the examples are in either C#
or VB. The namespace that works for C#:

using namespace System::Threading;

does not support the "CSingleLock singleLock(&m_CritSection);" that is
shown in some examples. And, it does not also support the "lock(this);"
shown in other examples. How is thread locking done with C++ in a
Windows form app?

TIA

Bill
 
R

Richard Grimes

bill said:
All,

I have a WindowsForm app and am happily getting data from my DLL which
is on a different thread than my UI. I have now arrived at the
situation where I need to lock the data that is being communicated.
Unhappily, again, I can find no examples using C++ to lock threads in
a .NET Windows Form application. All of the examples are in either C#
or VB. The namespace that works for C#:

using namespace System::Threading;

does not support the "CSingleLock singleLock(&m_CritSection);" that is
shown in some examples. And, it does not also support the
"lock(this);"
shown in other examples.


Take a look at code in C# that locks data using something like ILDASM.
This code:

lock(this)
{
//some code
}

is implemented like this:

Monitor.Enter(this);
try
{
//some code
}
finally
{
Monitor.Exit(this);
}

lock() is just syntactic sugar, you can use Monitor in your own code.
How is thread locking done with C++ in a
Windows form app?

The only think I should warn you (but I guess you know anyway) is that
you should never use a blocking thread synchronization on the GUI
thread. The reason is that when the thread is blocked it will not be
able to update the UI and as a consequence you'll find that the UI will
freeze.

Richard
 
B

bill

Richard,

Coincidentally, after my posting and before your response arrived I was
reading your excellent book "Programming with Managed Extensions for MS
Visual C++ .NET". Actually I poke around in it from time to time
looking for various things. In any case, I found your example on page
240 of using the ReaderWriterLock() to be clear and to the point. I
will say that I needed to stick a "return true" into your Data() class
before it would compile.

All of that being said, I finally ended up with the following:

if (0 == Interlocked::Exchange(&usingResource, 1))
{
// do stuff

//Release the lock
Interlocked::Exchange( &usingResource, 0);
}

Thanks for your response,

Bill
 
D

David Browne

Richard Grimes said:
Take a look at code in C# that locks data using something like ILDASM.
This code:

lock(this)
{
//some code
}

is implemented like this:

Monitor.Enter(this);
try
{
//some code
}
finally
{
Monitor.Exit(this);
}

lock() is just syntactic sugar, you can use Monitor in your own code.

Further, typically in C++ a wrapper object with a destructor is used to
manage resource lifetime. In C++/CLI an object with a destructor or a
Disposable object declared in automatic storage will be automatically
cleaned up as it leaves scope. This is similar to the using block of C#.
EG


#include "stdafx.h"

using namespace System;

ref class Lock
{
Object^ o;

~Lock()
{
System::Threading::Monitor::Exit(o);
Console::WriteLine("Monitor Exited.");
}

public:
Lock(Object^ objectToLock)
{
o = objectToLock;
System::Threading::Monitor::Enter(o);
Console::WriteLine("Monitor Entered.");
}
};

int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
Object^ o = gcnew Object();

{
Lock lock(o);
Console::WriteLine("Automatic Lock is in scope");
}

Console::WriteLine("Automatic Lock is out of scope.");

Console::ReadKey();

return 0;
}

Outputs:

Hello World
Monitor Entered.
Automatic Lock is in scope
Monitor Exited.
Automatic Lock is out of scope.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top