| |

VerySource

 Forgot password?
 Register
Search
View: 2067|Reply: 23

Asking strange questions about threads

[Copy link]

1

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-1-14 17:40:01
| Show all posts |Read mode
lngNowNumber = 0
        thr1 (0) = New Thread (AddressOf thr0)
        thr1 (0) .Start ()
        lngNowNumber + = 1
        thr1 (1) = New Thread (AddressOf thr1)
        thr1 (1) .Start ()
        lngNowNumber + = 1
        thr1 (2) = New Thread (AddressOf thr2)
        thr1 (2) .Start ()
        lngNowNumber + = 1
        thr1 (3) = New Thread (AddressOf thr3)
        thr1 (3) .Start ()
        lngNowNumber + = 1
        thr1 (4) = New Thread (AddressOf thr4)
        thr1 (4) .Start ()
        lngNowNumber + = 1
        thr1 (5) = New Thread (AddressOf thr5)
        thr1 (5) .Start ()
        lngNowNumber + = 1
        thr1 (6) = New Thread (AddressOf thr6)
        thr1 (6) .Start ()
        lngNowNumber + = 1
        thr1 (7) = New Thread (AddressOf thr7)
        thr1 (7) .Start ()
The operation of the main variables of the thread (the content of all threads is consistent):
sub thr0
do while lngnownumber <1000
       SyncLock Me
           lngNowNumber + = 1
           debug.print (lngNowNumber)
       End SyncLock
       'The following omitted N rows of data processing statements, which is time-consuming
loop
end sub
A total of 8 threads process the public variable lngNowNumber

Output results:
Thread 6: 15
Thread 4: 17
Thread 0: 18
Thread 2: 19
Thread 5: 20
Thread 3: 21
Thread 1: 22
Thread 0: 23
Thread 2: 24
Thread 6: 25
Thread 5: 26
Thread 1: 27
Thread 3: 28
Thread 4: 29
Thread 0: 30
Thread 5: 32
Thread 0: 33
Thread 1: 34
Thread 4: 35
Thread 2: 36
Thread 6: 37
Thread 5: 38
Thread 3: 39
Thread 4: 40
Thread 2: 41
Thread 3: 42
Thread 6: 43
Thread 1: 45
Thread 6: 47
Thread 4: 49
Thread 2: 50
Thread 0: 51
Thread 5: 52
Thread 3: 53
Thread 1: 54
Thread 0: 55
Thread 2: 56
Thread 6: 57
Thread 5: 58
Thread 1: 59
Thread 4: 60
Thread 3: 61
Thread 0: 63
Thread 0: 64
Thread 5: 65
Thread 4: 66
Thread 1: 67
Thread 6: 68
Thread 2: 69
Thread 3: 70
Thread 5: 71
Thread 2: 72
Thread 4: 73
Thread 6: 74
Thread 3: 75
Thread 1: 76
Thread 6: 78
Thread 4: 80
Thread 2: 82
Thread 0: 83
Thread 5: 84
Thread 3: 85
Thread 1: 86
Thread 0: 87
Thread 2: 88
Thread 6: 89
Thread 5: 90
Thread 1: 91
Thread 4: 92
Thread 3: 93
Thread 0: 95
Thread 0: 96
Thread 5: 97
Thread 1: 98
Thread 4: 99
Thread 2: 100
Thread 6: 101
Thread 3: 102
Thread 5: 103
Thread 4: 104
Thread 2: 105
Thread 3: 106
Thread 6: 107
Thread 1: 109
Thread 6: 111
Thread 4: 113
Thread 0: 114
Thread 2: 115
Thread 3: 116
Thread 5: 117
Thread 0: 118
Thread 1: 119
Thread 6: 120

problem:
1. Thread 7 is not executed (it is executed together with other threads), and it is output only at the end (only output after other threads are executed)
Thread 7: 1000
Thread 7: 1000
2. Why are some lngNowNumber skipped? (Note that the numbers output above are not continuous and there are gaps)
3. Tested
for i = 0 to 8
    thr1 (i) = New Thread (AddressOf thr1)
    thr1 (i) .Start ()
next
The execution result is different from the separate addressof above
    thr1 (0) = New Thread (AddressOf thr0)
    thr1 (). Start ()
Why is that?
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-18 20:36:01
| Show all posts
This is the characteristic of multi-threading. Who said that it starts first and ends first, and we want to start together?

Basically random.
Reply

Use magic Report

1

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-1-19 11:00:02
| Show all posts
No one asked to start or end together ... Don't take a bull
Reply

Use magic Report

1

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-1-19 12:54:01
| Show all posts
The problem is that thread 8 is not executed at all, and the first 7 are normal, that is, the last thread to be started will not be executed until other threads have finished executing. Is there a reason for this?
The other is to skip some variables, why is this?
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-1-20 10:18:02
| Show all posts
Make a mark first, and help you see it tomorrow ..
Reply

Use magic Report

1

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-1-22 20:36:01
| Show all posts
The above problems 1 and 3 have been solved ... The second problem is left ...

Find the synchronization information of msdn in detail
I found that ReaderWriterLock should be able to guarantee the reading and writing of variables, but it does not seem to understand how to use this thing. . . .

How to ensure that only one thread is reading a variable
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-23 16:00:01
| Show all posts
My idea: Add a BOOL variable, which is equivalent to a variable lock. When a ready-to-read variable is set, the BOOL variable is set to TRUE, and the state is restored after release, so that only one thread can read a variable Up
Reply

Use magic Report

0

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-23 17:45:01
| Show all posts
lngNowNumber + = 1
           debug.print (lngNowNumber)
After the increment and before the debug output, the value of lngNowNumber is changed by another thread, so the value jumps.
Reply

Use magic Report

0

Threads

23

Posts

20.00

Credits

Newbie

Rank: 1

Credits
20.00

 China

Post time: 2020-1-23 20:09:01
| Show all posts
Add lock
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-24 03:09:01
| Show all posts
Take notes
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