high6 Posted May 21, 2009 Posted May 21, 2009 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 May 22, 2009 Posted May 22, 2009 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.
Ufo-Pu55y Posted May 22, 2009 Posted May 22, 2009 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 asvoid SomeMethod() { object oTemp = this; Monitor.Enter(oTemp); try { // do some stuff } finally { Monitor.Exit(oTemp); }}peter already told about its use cases.
high6 Posted May 22, 2009 Author Posted May 22, 2009 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?
atom0s Posted May 29, 2009 Posted May 29, 2009 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 - LeaveCriticalSectionand others.More information:http://msdn.microsoft.com/en-us/library/ms682530(VS.85).aspxAnd for more info on lock in C#:http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.71).aspx
Majii Guy Posted May 30, 2009 Posted May 30, 2009 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 - LeaveCriticalSectionand others.More information:http://msdn.microsoft.com/en-us/library/ms682530(VS.85).aspxAnd for more info on lock in C#:http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.71).aspxThough, 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.
atom0s Posted May 30, 2009 Posted May 30, 2009 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.
Majii Guy Posted May 30, 2009 Posted May 30, 2009 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).
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now