I'm writing quite simple synchronized code and I'm really stuck on this error I get. What could cause the error?
Program throws error at last line on PulseAll(processingText)
UPDATE: there is also other function in my monitoring class which is called by main thread ProcessingText(), could it cause the issue?
public void doWork(int threadTaskNumber)
{
while (!(bool)WorkDone)
{
lock (processingText)
{
while (vowelCounter != 3 && threadTaskNumber != 0)
{
Monitor.Wait(processingText);
}
if (!(bool)workDone)
{
switch (threadTaskNumber)
{
case 0:
processingText += 'A';
if (vowelCounter != 3)
vowelCounter++;
break;
case 1:
processingText += 'B';
break;
case 2:
processingText += 'C';
break;
}
if (++threadCounter == 15)
WorkDone = true;
}
Monitor.PulseAll(processingText);
}
}
}
public void ProcessingText()
{
lock (processingText)
{
Console.WriteLine(processingText);
}
}
The SynchronizationLockException
is thrown because between these two lines:
Monitor.Wait(processingText);
//...
Monitor.PulseAll(processingText);
...the value of the variable processingText
has changed. So the Monitor.PulseAll
tries to send a pulse using a lock that is not owned by the current thread, hence the exception you observe.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.