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

thread synchronization

Hi,

how can I synchronize 2 threads using the 'lock'-equivalent that is available in C# ?

thanks
Chris

************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 17 '05 #1
8 1669
Chris C wrote:
Hi,

how can I synchronize 2 threads using the 'lock'-equivalent that is
available in C# ?


Managed code or native?

If managed, you can use the same mechanism that C# uses: the
System.Threading.Monitor class - you just have to call the member functions
explicitly while the C# lock construct calls them for you.

If native, depending on what you need, you might use a CriticalSection,
Mutex, Semphore or Event.

http://msdn.microsoft.com/library/de...on_objects.asp

is a good jumping-off point for information about the kernel synchronization
objects (Mutex, Semaphore, Event), while

http://msdn.microsoft.com/library/de...on_objects.asp

covers CriticalSections (which are not kernel objects - they're implemented
by the win32 subsystem).

-cd
Nov 17 '05 #2
Hi Carl!
how can I synchronize 2 threads using the 'lock'-equivalent that is
available in C# ?


If managed, you can use the same mechanism that C# uses: the
System.Threading.Monitor class - you just have to call the member functions
explicitly while the C# lock construct calls them for you.


Just as an addition, here is a small code-sample (because this will be
mostly implemented without the __try-__finally):

C#:
lock(obj)
{
// Some code
}

managed C++:
System::Threading::Monitor::Enter(obj);
__try
{
// Some code
}
__finally
{
System::Threading::Monitor::Exit(obj);
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #3
The technique of try finally is grate but the following code is a more C++
style aproach:

#include <windows.h>

template<typename LOCK_TYPE>
class Guard
{
protected:
LOCK_TYPE * lock_;
public:
Guard(LOCK_TYPE & lock) : lock_(&lock)
{
this->lock_->aquire();
}

~Guard()
{
this->lock_->release();
}
};

class CriticalSection
{
private:
CRITICAL_SECTION cs_;

public:
CriticalSection()
{
::InitializeCriticalSection(&this->cs_);
}

~CriticalSection()
{
::DeleteCriticalSection(&this->cs_);
}

void aquire(void)
{
::EnterCriticalSection(&this->cs_);
}

void release(void)
{
::LeaveCriticalSection(&this->cs_);
}
};

class Mutex
{
// kind of the same as CritcalSection class but using CreateMutex,
OpenMutex, etc...
};

CriticalSection g_cs;
//Mutex g_mtx;

int do_something(void)
{
Guard<CriticalSection> guard(g_cs);
//Guard<Mutex> guard(g_mtx);

// do something usefull and dont't butter about releasing the critical
// section because the Guard's template destructor will do it for you
// as soon as you leave this function whatever it happens.

// it is pretty generic because you can use not only critical sections
// but mutexes as well.

return 0;
}

int main(int argc, char * argv[])
{
return do_something();
}

Ivan Mejia C++ Dev.

"Jochen Kalmbach [MVP]" wrote:
Hi Carl!
how can I synchronize 2 threads using the 'lock'-equivalent that is
available in C# ?


If managed, you can use the same mechanism that C# uses: the
System.Threading.Monitor class - you just have to call the member functions
explicitly while the C# lock construct calls them for you.


Just as an addition, here is a small code-sample (because this will be
mostly implemented without the __try-__finally):

C#:
lock(obj)
{
// Some code
}

managed C++:
System::Threading::Monitor::Enter(obj);
__try
{
// Some code
}
__finally
{
System::Threading::Monitor::Exit(obj);
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 17 '05 #4
Hi Ivan!
The technique of try finally is grate but the following code is a more C++
style aproach:


You are right, but my intention was pure managed C++... and because of
the missing "using" keyword it is not possible to implement this in an
__gc class...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #5
The technique of try finally is grate but the following code is a more C++
style aproach:

#include <windows.h>

template<typename LOCK_TYPE>
class Guard
{
protected:
LOCK_TYPE * lock_;
public:
Guard(LOCK_TYPE & lock) : lock_(&lock)
{
this->lock_->aquire();
}

~Guard()
{
this->lock_->release();
}
};

class CriticalSection
{
private:
CRITICAL_SECTION cs_;

public:
CriticalSection()
{
::InitializeCriticalSection(&this->cs_);
}

~CriticalSection()
{
::DeleteCriticalSection(&this->cs_);
}

void aquire(void)
{
::EnterCriticalSection(&this->cs_);
}

void release(void)
{
::LeaveCriticalSection(&this->cs_);
}
};

class Mutex
{
// kind of the same as CritcalSection class but using CreateMutex,
OpenMutex, etc...
};

CriticalSection g_cs;
//Mutex g_mtx;

int do_something(void)
{
Guard<CriticalSection> guard(g_cs);
//Guard<Mutex> guard(g_mtx);

// do something usefull and dont't butter about releasing the critical
// section because the Guard's template destructor will do it for you
// as soon as you leave this function whatever it happens.

// it is pretty generic because you can use not only critical sections
// but mutexes as well.

return 0;
}

int main(int argc, char * argv[])
{
return do_something();
}

Ivan Mejia C++ Dev.

"Jochen Kalmbach [MVP]" wrote:
Hi Carl!
how can I synchronize 2 threads using the 'lock'-equivalent that is
available in C# ?


If managed, you can use the same mechanism that C# uses: the
System.Threading.Monitor class - you just have to call the member functions
explicitly while the C# lock construct calls them for you.


Just as an addition, here is a small code-sample (because this will be
mostly implemented without the __try-__finally):

C#:
lock(obj)
{
// Some code
}

managed C++:
System::Threading::Monitor::Enter(obj);
__try
{
// Some code
}
__finally
{
System::Threading::Monitor::Exit(obj);
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 17 '05 #6
Hi Ivan!
The technique of try finally is grate but the following code is a more C++
style aproach:


You are right, but my intention was pure managed C++... and because of
the missing "using" keyword it is not possible to implement this in an
__gc class...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #7
Hi Jochen!
Ok and sorry but my intesion was the same as yours, to provide a soluction
but in this case using nothing but plane C++.

Take care!

"Jochen Kalmbach [MVP]" wrote:
Hi Ivan!
The technique of try finally is grate but the following code is a more C++
style aproach:


You are right, but my intention was pure managed C++... and because of
the missing "using" keyword it is not possible to implement this in an
__gc class...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 17 '05 #8
Hi Jochen!
Ok and sorry but my intesion was the same as yours, to provide a soluction
but in this case using nothing but plane C++.

Take care!

"Jochen Kalmbach [MVP]" wrote:
Hi Ivan!
The technique of try finally is grate but the following code is a more C++
style aproach:


You are right, but my intention was pure managed C++... and because of
the missing "using" keyword it is not possible to implement this in an
__gc class...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 17 '05 #9

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

Similar topics

5
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
1
by: Bill Davidson | last post by:
(RESEND: I added a little more code to the sample for clarity) Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things,...
4
by: scott | last post by:
hi all, Thx to any one that can offer me help, it will be much appreciated. iv got a multithreaded program and need to use thread synchronization. The synchronization does not have to...
20
by: Bob Day | last post by:
Using VS 2003, VB, MSDE... There are two threads, A & B, that continously run and are started by Sub Main. They instantiationsl of identical code. Thread A handles call activity on telephone...
6
by: Robert Speck | last post by:
Hi there, Can anyone shed anymore light on why "Thread.Suspend()" has been deprecated by MSFT beyond what MSDN says about it. I'm not sure if I quite appreciate the various pitfalls they discuss...
13
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
0
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing...
19
by: Hapa | last post by:
Does only reading (never writing) of a variable need thread synchronisation? Thanks for help? PS. Anybody knows a Visual C++ news group?
8
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.