G**T 发帖数: 388 | 1 I'm using a function to record all the exception in the program. However,
sometimes, it reports that: can not access the file (gstrErrorFileName), since
another process is using it. In my program, only the code below accesses that
file and I use synclock to lock it. Could it because windows OS is accessing
it? If yes, how can I prevent it so that it won't throw the exception?
thanks
visual basic code:
' write to the error log file
Dim oLockFile As New Object
SyncLock oLoc |
|
q********y 发帖数: 162 | 2 public class Foo
{
private const int SyncLock = 1 << 10;
public void Execute()
{
lock((object)SyncLock)
{
//complex logic
}
}
} |
|
k**********g 发帖数: 989 | 3
SyncLock is a primitive value. Strictly speaking, it is not even a value,
because it is a compile time constant. If you load this value into a CPU
register, the generated code will just write the constant 1024. No memory
loads.
When you do a ((object)SyncLock), this is called boxing. Boxing creates a
new object on the heap, with enough space for storing a copy of the value.
so, it is roughly similar to this in C++ :
struct BoxedInteger { int value; }
BoxedInteger* pObject = new BoxedInteger;
pO... 阅读全帖 |
|
d******i 发帖数: 7160 | 4 代码类似于;
class LoadBalanceServer
{
private const int SERVER_COUNT = 3;
private List serverList = new List();
private static volatile LoadBalanceServer lbs;
private static object syncLock = new object();
...
}
注意 这个 List serverList 不是静态的。
首先,这个new 何时发生的?
那么他的这个"new"是
定义这个类的时候就发生了,
还是被client 构造 LoadBalanceServer 的 instance时才发生的呢?
还有如题,in either case,new之后serverList占据了谁的内存块?作为instance
object的
一部分挤在heap上,还是...
请指教。
谢谢! |
|