| |

VerySource

 Forgot password?
 Register
Search
Author: irwin8888

Asking strange questions about threads

[Copy link]

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-2-21 09:45:01
| Show all posts
Output:
thread0: 1
thread6: 2
thread6: 3
thread6: 4
thread6: 5
thread6: 6
thread6: 7
thread6: 8
thread6: 9
thread1: 10
thread1: 11
thread0: 12
thread2: 13
thread2: 14
thread3: 15
thread4: 16
thread4: 17
thread4: 18
thread5: 19
thread5: 20
thread5: 21
thread0: 22
thread1: 23
thread2: 24
thread3: 25
thread4: 26
thread0: 27
thread1: 28
thread2: 29
thread0: 30
thread1: 31
thread2: 32
thread0: 33
thread1: 34
thread2: 35
thread0: 36
thread1: 37
thread2: 38
thread0: 39
thread1: 40
thread2: 41
thread0: 42
thread1: 43
thread2: 44
thread0: 45
thread1: 46
thread2: 47
thread0: 48
thread1: 49
thread2: 50
thread0: 51
thread1: 52
thread2: 53
thread0: 54
thread1: 55
thread2: 56
thread0: 57
thread1: 58
thread2: 59
thread0: 60
thread1: 61
thread2: 62
thread0: 63
thread1: 64
thread2: 65
thread0: 66
thread1: 67
thread2: 68
thread0: 69
thread1: 70
thread2: 71
thread0: 72
thread1: 73
thread2: 74
thread0: 75
thread1: 76
thread2: 77
thread0: 78
thread1: 79
thread2: 80
thread0: 81
thread1: 82
thread2: 83
thread0: 84
thread1: 85
thread2: 86
thread0: 87
thread1: 88
thread2: 89
thread0: 90
...
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-2-21 21:30:01
| Show all posts
TO: lock only guarantees that there will not be multiple threads writing to a certain code at the same time
But there is no guarantee that only one thread will read the same code


This happens because:
public static void Run ()
{
lock (thisLock)
{
while (lNum <100)
{
lNum ++;
Console.WriteLine ("Thread {0}: lNum = {1}", Thread.CurrentThread.Name, lNum);
}
}

The while loop is in the lock.For example, thread 1 gets the mutex of the thread, then other threads cannot access the code inside:

while (lNum <100)
{}
So after thread 1 gets the mutex, it runs the while loop, and it will not release the mutex until the loop reaches 100. When other threads get the mutex, the runtime finds that lNum is already 100, so they all quit.

And if:

while (count <= 100)
            {
                mx.WaitOne ();

                count ++;
                Debug.WriteLine ("thread" + Thread.CurrentThread.Name + ":" + count.ToString ());
                Thread.Sleep (100);

                mx.ReleaseMutex ();
            }
That is, the while loop is outside, the effect is not the same, it gets the mutex right inside the loop, gets the mutex right before incrementing 1, and releases the mutex right after incrementing 1, so each thread will have the opportunity to increase the variable by 1. ..
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-2-22 01:00:02
| Show all posts
The example I wrote above can only explain the problem, but it is actually not good enough ...

Although it is while (count <= 100), in fact, the value of count may reach 101 at the end. The specific reason can be analyzed by yourself. If you are not clear, you can discuss it with me ..
Reply

Use magic Report

1

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-4-9 13:15:02
| Show all posts
I really don't know why it outputs 101 ...
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list