Jump to content
Tuts 4 You

Lock prefix?


high6

Recommended Posts

Posted

What is this used for? And is there C++/C code that when compiled uses this(to give a better understanding of it)?

I checked the help file but I don't really get what it is used for. Is there an unlock?

I am probably getting confused due to the lack of knowing more about how the cpu/etc interact, etc.

Peter Ferrie
Posted

It allows an instruction to complete without any chance of interruption. There is no need for an "unlock" because the lock lasts only as long as the instruction takes to execute.

This is very important for multi-CPU environments, since it allows one CPU to guarantee that it has written the required value to that memory location, before any other CPU can either read or write that same memory location.

Posted

recently read about it. it's another instruction to get cleaner/shorter syntax... just like 'using' for example.

void SomeMethod() {
lock (this) {
// do some stuff
}
}

will generate the same code as

void SomeMethod() {
object oTemp = this;
Monitor.Enter(oTemp);
try {
// do some stuff
}
finally {
Monitor.Exit(oTemp);
}
}

peter already told about its use cases.

Posted

Okay I think I get it.

So the C++ one sets a bool using the lock instruction so that no other cpus access it before the lock is in place?

Posted
What is this used for? And is there C++/C code that when compiled uses this(to give a better understanding of it)?

I checked the help file but I don't really get what it is used for. Is there an unlock?

I am probably getting confused due to the lack of knowing more about how the cpu/etc interact, etc.

C++ has the similar effect using API:

- EnterCriticalSection

- LeaveCriticalSection

and others.

More information:

http://msdn.microsoft.com/en-us/library/ms682530(VS.85).aspx

And for more info on lock in C#:

http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.71).aspx
Posted
What is this used for? And is there C++/C code that when compiled uses this(to give a better understanding of it)?

I checked the help file but I don't really get what it is used for. Is there an unlock?

I am probably getting confused due to the lack of knowing more about how the cpu/etc interact, etc.

C++ has the similar effect using API:

- EnterCriticalSection

- LeaveCriticalSection

and others.

More information:

http://msdn.microsoft.com/en-us/library/ms682530(VS.85).aspx

And for more info on lock in C#:

http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.71).aspx

Though, critical sections are limited to Windows environments (Which is a given, due to the link to MSDN), where as the LOCK or Try/Finally statement should function cross-platform.

Posted
Though, critical sections are limited to Windows environments (Which is a given, due to the link to MSDN), where as the LOCK or Try/Finally statement should function cross-platform.

Users on other platforms can use their OS specific similar API/functions to handle the differences. While the names of things wont be the same, the overall purpose and result will be similar. Such as, Linux users can use the following to do the similar effect of critical sections:

EnterCriticalSection (win32) -> pthread_mutex_lock (Linux)

LeaveCriticalSection (win32) -> pthread_mutex_trylock (Linux)

DeleteCriticalSection (win32) -> pthread_mutex_destroy (Linux)

There will be slight differences doing it that way since win32 critical sections are specific to the process using the API, while mutex threads can go cross-process. But you can construct functions to pretty much emulate the critical section API on win32 to prevent cross-process access if the need is there.

I mainly pointed out the critical sections based on high6's question, since .NET (C# lock) is specific to Win32.

Posted
Though, critical sections are limited to Windows environments (Which is a given, due to the link to MSDN), where as the LOCK or Try/Finally statement should function cross-platform.

Users on other platforms can use their OS specific similar API/functions to handle the differences. While the names of things wont be the same, the overall purpose and result will be similar. Such as, Linux users can use the following to do the similar effect of critical sections:

EnterCriticalSection (win32) -> pthread_mutex_lock (Linux)

LeaveCriticalSection (win32) -> pthread_mutex_trylock (Linux)

DeleteCriticalSection (win32) -> pthread_mutex_destroy (Linux)

There will be slight differences doing it that way since win32 critical sections are specific to the process using the API, while mutex threads can go cross-process. But you can construct functions to pretty much emulate the critical section API on win32 to prevent cross-process access if the need is there.

I mainly pointed out the critical sections based on high6's question, since .NET (C# lock) is specific to Win32.

I well understood your purpose, I was just leaving notes for future users whom may be writing on a non-Windows platform (i.e.: Any number of Linux or UNIX distro, among other possible environments).

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...