C# 创建等待var更改的线程
我希望有一个线程,它从一个队列和多个线程中消费,生成工作并将其放在这个队列上,然后允许原始的生成线程能够等待(在某个时刻)完成工作并继续处理对象 像这样的事情:C# 创建等待var更改的线程,c#,multithreading,c#-4.0,thread-safety,C#,Multithreading,C# 4.0,Thread Safety,我希望有一个线程,它从一个队列和多个线程中消费,生成工作并将其放在这个队列上,然后允许原始的生成线程能够等待(在某个时刻)完成工作并继续处理对象 像这样的事情: loop: 1. TheThread waiting for "myObj pending" is not null. 2. Thread2 changing "pending" object. 3.1. TheThread do some stuff on "pending" 3.2. Thread2 doing some anoth
loop:
1. TheThread waiting for "myObj pending" is not null.
2. Thread2 changing "pending" object.
3.1. TheThread do some stuff on "pending"
3.2. Thread2 doing some another stuff.
4. Thread2 waiting until Thread finished, and then do something on "pending" and return him to be null
[there is many "Thread"s like "Thread2", and I want it to be ThreadSafe]
我试图在下面的代码中这样做,但这是我第一次使用线程,所以我不确定我做错了什么,是否有有效的方法
ManualResetEvent mre = new ManualResetEvent(false);
myObj pending = null;
Thread worker = new Thread(doWork);
Thread.start();
Thread Thread2 = new Thread(anotherMethod);
Thread Thread3 = new Thread(anotherMethod2);
void doWork()
{
while (true)
{
if (pending == null)
{
mre.waitOne()
}
lock(pending)
{
pending.doSomething();
mre = new ManualResetEvent(false);
}
}
}
void anotherMethod()
{
//doStuff
pending = new myObj()
mre.set();
//doStuff
worker.Join()
pending.doSomeThingJustIfDoWorkDone()
}
void anotherMethod2()
{
//doStuff
pending = new myObj()
mre.set();
//doStuff
worker.Join()
pending.doSomeThingJustIfDoWorkDone()
}
您可以调用以等待工作线程完成。您可以连接多个线程
我认为最好使用而不是手动处理线程。您可以调用以等待工作线程完成。您可以连接多个线程
我认为最好使用而不是手动处理线程。您可以调用以等待工作线程完成。您可以连接多个线程
我认为最好使用而不是手动处理线程。您可以调用以等待工作线程完成。您可以连接多个线程
我认为最好使用而不是手动处理线程。您应该锁定另一个对象,因为您所做的操作可能会导致死锁 与其使用挂起的对象,不如使用
private readonly Object _myPendingLock = new Object();
无论何时调用挂起对象,都应该锁定此对象
void anotherMethod()
{
lock(_myPendingLock)
//doStuff
pending = new myObj()
mew.set();
//doStuff
[...]
worker.Join()
pending.doSomeThingJustIfDoWorkDone()
}
}
//但是如果你想让生产者消费者拥有最安全的男人,最好看看内置类
您应该锁定另一个对象,因为您所做的操作可能会导致死锁 与其使用挂起的对象,不如使用
private readonly Object _myPendingLock = new Object();
无论何时调用挂起对象,都应该锁定此对象
void anotherMethod()
{
lock(_myPendingLock)
//doStuff
pending = new myObj()
mew.set();
//doStuff
[...]
worker.Join()
pending.doSomeThingJustIfDoWorkDone()
}
}
//但是如果你想让生产者消费者拥有最安全的男人,最好看看内置类
您应该锁定另一个对象,因为您所做的操作可能会导致死锁 与其使用挂起的对象,不如使用
private readonly Object _myPendingLock = new Object();
无论何时调用挂起对象,都应该锁定此对象
void anotherMethod()
{
lock(_myPendingLock)
//doStuff
pending = new myObj()
mew.set();
//doStuff
[...]
worker.Join()
pending.doSomeThingJustIfDoWorkDone()
}
}
//但是如果你想让生产者消费者拥有最安全的男人,最好看看内置类
您应该锁定另一个对象,因为您所做的操作可能会导致死锁 与其使用挂起的对象,不如使用
private readonly Object _myPendingLock = new Object();
无论何时调用挂起对象,都应该锁定此对象
void anotherMethod()
{
lock(_myPendingLock)
//doStuff
pending = new myObj()
mew.set();
//doStuff
[...]
worker.Join()
pending.doSomeThingJustIfDoWorkDone()
}
}
//但是如果你想让生产者消费者拥有最安全的男人,最好看看内置类
在处理线程时,我更喜欢使用任务来管理它们 我认为任务并行库中的ContinueWith方法就是您正在寻找的方法。看看这些例子,也许这可以帮助你
在处理线程时,我更喜欢使用任务来管理它们 我认为任务并行库中的ContinueWith方法就是您正在寻找的方法。看看这些例子,也许这可以帮助你
在处理线程时,我更喜欢使用任务来管理它们 我认为任务并行库中的ContinueWith方法就是您正在寻找的方法。看看这些例子,也许这可以帮助你
在处理线程时,我更喜欢使用任务来管理它们 我认为任务并行库中的ContinueWith方法就是您正在寻找的方法。看看这些例子,也许这可以帮助你
4.5中有
ConcurrentExclusiveSchedulerPair
,它正是您想要的
一般来说,您的用例在里克特的书《通过C#的CLR》中有描述,在变量模式中称为条件
internal sealed class ConditionVariablePattern {
private readonly Object m_lock = new Object();
private Boolean m_condition = false;
public void Thread1() {
Monitor.Enter(m_lock); // Acquire a mutual-exclusive lock
// While under the lock, test the complex condition "atomically"
while (!m_condition) {
// If condition is not met, wait for another thread to change the condition
Monitor.Wait(m_lock); // Temporarily release lock so other threads can get it
}
// The condition was met, process the data...
Monitor.Exit(m_lock); // Permanently release lock
}
public void Thread2() {
Monitor.Enter(m_lock); // Acquire a mutual-exclusive lock
// Process data and modify the condition...
m_condition = true;
// Monitor.Pulse(m_lock); // Wakes one waiter AFTER lock is released
Monitor.PulseAll(m_lock); // Wakes all waiters AFTER lock is released
Monitor.Exit(m_lock); // Release lock
}
}
4.5中有一个ConcurrentExclusiveSchedulerPair
,它正是您想要的
一般来说,您的用例在里克特的书《通过C#的CLR》中有描述,在变量模式中称为条件
internal sealed class ConditionVariablePattern {
private readonly Object m_lock = new Object();
private Boolean m_condition = false;
public void Thread1() {
Monitor.Enter(m_lock); // Acquire a mutual-exclusive lock
// While under the lock, test the complex condition "atomically"
while (!m_condition) {
// If condition is not met, wait for another thread to change the condition
Monitor.Wait(m_lock); // Temporarily release lock so other threads can get it
}
// The condition was met, process the data...
Monitor.Exit(m_lock); // Permanently release lock
}
public void Thread2() {
Monitor.Enter(m_lock); // Acquire a mutual-exclusive lock
// Process data and modify the condition...
m_condition = true;
// Monitor.Pulse(m_lock); // Wakes one waiter AFTER lock is released
Monitor.PulseAll(m_lock); // Wakes all waiters AFTER lock is released
Monitor.Exit(m_lock); // Release lock
}
}
4.5中有一个ConcurrentExclusiveSchedulerPair
,它正是您想要的
一般来说,您的用例在里克特的书《通过C#的CLR》中有描述,在变量模式中称为条件
internal sealed class ConditionVariablePattern {
private readonly Object m_lock = new Object();
private Boolean m_condition = false;
public void Thread1() {
Monitor.Enter(m_lock); // Acquire a mutual-exclusive lock
// While under the lock, test the complex condition "atomically"
while (!m_condition) {
// If condition is not met, wait for another thread to change the condition
Monitor.Wait(m_lock); // Temporarily release lock so other threads can get it
}
// The condition was met, process the data...
Monitor.Exit(m_lock); // Permanently release lock
}
public void Thread2() {
Monitor.Enter(m_lock); // Acquire a mutual-exclusive lock
// Process data and modify the condition...
m_condition = true;
// Monitor.Pulse(m_lock); // Wakes one waiter AFTER lock is released
Monitor.PulseAll(m_lock); // Wakes all waiters AFTER lock is released
Monitor.Exit(m_lock); // Release lock
}
}
4.5中有一个ConcurrentExclusiveSchedulerPair
,它正是您想要的
一般来说,您的用例在里克特的书《通过C#的CLR》中有描述,在变量模式中称为条件
internal sealed class ConditionVariablePattern {
private readonly Object m_lock = new Object();
private Boolean m_condition = false;
public void Thread1() {
Monitor.Enter(m_lock); // Acquire a mutual-exclusive lock
// While under the lock, test the complex condition "atomically"
while (!m_condition) {
// If condition is not met, wait for another thread to change the condition
Monitor.Wait(m_lock); // Temporarily release lock so other threads can get it
}
// The condition was met, process the data...
Monitor.Exit(m_lock); // Permanently release lock
}
public void Thread2() {
Monitor.Enter(m_lock); // Acquire a mutual-exclusive lock
// Process data and modify the condition...
m_condition = true;
// Monitor.Pulse(m_lock); // Wakes one waiter AFTER lock is released
Monitor.PulseAll(m_lock); // Wakes all waiters AFTER lock is released
Monitor.Exit(m_lock); // Release lock
}
}
这不是我的问题。。我的问题是如何使它线程安全。。我将问题编辑得更具体一些@ZMBQ有许多线程通过更改“挂起”来调用doWork,我希望任何“希望”使用“doWork”的线程都将等待,直到“挂起”为空(意思是:doWork是免费的,正在等待)@ZMBQ这不是我的问题。。我的问题是如何使它线程安全。。我将问题编辑得更具体一些@ZMBQ有许多线程通过更改“挂起”来调用doWork,我希望任何“希望”使用“doWork”的线程都将等待,直到“挂起”为空(意思是:doWork是免费的,正在等待)@ZMBQ这不是我的问题。。我的问题是如何使它线程安全。。我将问题编辑得更具体一些@ZMBQ有许多线程通过更改“挂起”来调用doWork,我希望任何“希望”使用“doWork”的线程都将等待,直到“挂起”为空(意思是:doWork是免费的,正在等待)@ZMBQ这不是我的问题。。我的问题是如何使它线程安全。。我将问题编辑得更具体一些@zmbqIt有许多线程通过更改“挂起”来调用doWork,我希望任何“想要”使用“doWork”的线程都将等待,直到“挂起”为空(意思是:doWork是空闲的,正在等待)@zmbqIt看起来(有点)像您试图做的是让一个线程从队列中消费多个线程