I have an application which run more than one thread at the sametime.
All those thread call same function (output function). How I lock this
function for other thread while one thread already use it.
in C# I done it by :
void output (String ff)
{
lock(this)
{
........
}
}
How to do that in VC++.Net ?
William DePalo [MVP VC++] - 08 Jun 2005 15:09 GMT
>I have an application which run more than one thread at the sametime.
> All those thread call same function (output function). How I lock this
[quoted text clipped - 9 lines]
>
> How to do that in VC++.Net ?
You need a critical section which is created with InitializeCritcalSection()
and destroyed with DeleteCritcalSection(). On entry to your "locked" block
you call EnterCriticalSection() and on exit LeaveCriticalSection().
Regards,
Will
Marcus Heege - 08 Jun 2005 18:38 GMT
The answer to your question heavily depends on what use and you want to do.
If you are using C++/CLI that comes with VS.NET 2005, and implement managed
code, you can include <msclr\lock.h> and use the class msclr::lock. If you
are using Managed C++, you have to call the static methode Enter/TryEnter
and Exit of System::Threading::Monitor. If you are writing native code, you
have to use a Win32 API like the critical section api.
Marcus Heege
>I have an application which run more than one thread at the sametime.
> All those thread call same function (output function). How I lock this
[quoted text clipped - 9 lines]
>
> How to do that in VC++.Net ?