让老人笑话的简单问题,来者有分!

乘思者 2006-01-27 03:52:34
一个变量,如何在使用的时候锁定它,防止其他的线程更改它,直到本线程认为处理结束。
Lock{}?
Monitor?

这样就可以了吗?

如何防止自己同一线程多次锁定?
不知道自己锁定了几次?
解的时候要解开该怎么办?

...全文
404 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangkc 2006-02-13
  • 打赏
  • 举报
回复

顺便学习
MonkWang 2006-02-13
  • 打赏
  • 举报
回复
不知道 帮顶
顺便学习
是是非非 2006-02-13
  • 打赏
  • 举报
回复
不懂,帮顶

个人感觉可能是 lock
乘思者 2006-02-13
  • 打赏
  • 举报
回复
我当前的实现,大家帮我看看有什么问题没?

//多线程同步锁
bool _locked;
public object SyncRoot
{
get
{
return _dd;
}
}
public void Lock(Guid documentGuid)
{
_dd.Current=documentGuid;

System.Threading.Monitor.Enter(SyncRoot);
if(_locked)
{
System.Threading.Monitor.Exit(SyncRoot);
}
else
{
_locked=true;
}
}

public void Unlock()
{
System.Threading.Monitor.Exit(SyncRoot);
System.Threading.Monitor.Exit(SyncRoot);
System.Threading.Monitor.Exit(SyncRoot);
_locked=false;
}

public bool Locked
{
get
{
return _locked;
}
}
冰河zyc 2006-02-06
  • 打赏
  • 举报
回复
没用过
帮顶
califord 2006-02-05
  • 打赏
  • 举报
回复
lock是在golbal.aspc页里的呀
乘思者 2006-02-04
  • 打赏
  • 举报
回复
我的原因是这样的:

我的一个类的一个目的有多种操作方式,而这几种操作方式是实施这个目的的不同步骤地入口,如果我锁定了,那么后面的步骤不知道前面的步骤已经锁定了,还会继续锁定(同一线程多次锁定),如果我不锁定,那样就会被其他线程更改掉。
qiyu20031022 2006-01-29
  • 打赏
  • 举报
回复
不懂,来学习.
LixingTie 2006-01-29
  • 打赏
  • 举报
回复
greenery好厉害,我也是来抢分的,嘿嘿
broadury 2006-01-29
  • 打赏
  • 举报
回复
学习……
dot net中好像有相应的锁类
zhy0101 2006-01-28
  • 打赏
  • 举报
回复
同一线程的代码是顺序执行的,可以避免多次lock
handsome0916 2006-01-28
  • 打赏
  • 举报
回复
不要将解锁放在阻塞的循环之外,执行完要立即解锁.要不是就会出现死锁.
假如要用多个锁,可以锁定几个不同的变量,这样就不会不同的锁间互相干侥
代码蜗牛sky 2006-01-27
  • 打赏
  • 举报
回复
lock
greenery 2006-01-27
  • 打赏
  • 举报
回复
按你的要求,可以在线程执行的函数中,一开始就开始锁定
在以下例子中,线程函数Withdraw在开始计算前,先将对将自己锁定,防止其他线程对其变量balance修改。

using System;
using System.Threading;

class Account
{
int balance;

Random r = new Random();

public Account(int initial)
{
balance = initial;
}

int Withdraw(int amount)
{

// This condition will never be true unless the lock statement
// is commented out:
if (balance < 0)
{
throw new Exception("Negative Balance");
}

// Comment out the next line to see the effect of leaving out
// the lock keyword:
lock (this)
{
if (balance >= amount)
{
Console.WriteLine("Balance before Withdrawal : " + balance);
Console.WriteLine("Amount to Withdraw : -" + amount);
balance = balance - amount;
Console.WriteLine("Balance after Withdrawal : " + balance);
return amount;
}
else
{
return 0; // transaction rejected
}
}
}

public void DoTransactions()
{
for (int i = 0; i < 100; i++)
{
Withdraw(r.Next(1, 100));
}
}
}

class Test
{
public static void Main()
{
Thread[] threads = new Thread[10];
Account acc = new Account (1000);
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(acc.DoTransactions));
threads[i] = t;
}
for (int i = 0; i < 10; i++)
{
threads[i].Start();
}
}
}


另一个例子
class MyClass
{
public static int ItemCount = 0;
}

class 线程1
{
public void AddItem()
{
lock(MyClass.ItemCount)
{
list.Add(...);
MyClass.ItemCount++;
}
}
}
class 线程2
{
public void RemoveItem()
{
lock(MyClass.ItemCount)
{
list.Remove(...);
MyClass.ItemCount--;
}
}
}
hotnoodle 2006-01-27
  • 打赏
  • 举报
回复
学习,接分
raulredondo 2006-01-27
  • 打赏
  • 举报
回复
不知道,学习,接分
菠菜Hello_World 2006-01-27
  • 打赏
  • 举报
回复
什么叫跨方法???
wdszya 2006-01-27
  • 打赏
  • 举报
回复
不知道,学习,接分
乘思者 2006-01-27
  • 打赏
  • 举报
回复
如果我的进程要跨方法运行呢?
xldlm 2006-01-27
  • 打赏
  • 举报
回复
lock
出了"}"自动解除
加载更多回复(2)

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧