| |

VerySource

 Forgot password?
 Register
Search
Author: irwin8888

Asking strange questions about threads

[Copy link]

1

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-1-27 11:00:01
| Show all posts
Adding a bool variable seems to work, but .net has given the readerwriterlock class. . . I don't think I should use this class
Reply

Use magic Report

1

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-1-28 19:00:02
| Show all posts
How to ensure that only one thread is reading global variables?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Brazil

Post time: 2020-2-8 02:00:01
| Show all posts
1.Use Monitor.Enter
2.lock
3. Separate the code that needs to be synchronized into a method, and add the MethodImpl attribute to the method
c # syntax:
[MethodImpl (MethodImplOptions.Synchronized)]
public void doSomething ()
{
  // Handle global variables
}
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 Australia

Post time: 2020-2-9 16:15:01
| Show all posts
public class T
{
public static long lNum = 0;
public static void Main ()
{
const int MaxThread = 7;
Thread [] arrt = new Thread [7];
for (int i = 0; i <MaxThread; i ++)
{
arrt [i] = new Thread (new ThreadStart (Run));
arrt [i] .Name = i.ToString ();
arrt [i] .Start ();
}
Ranch
}
private static object thisLock = new object ();
public static void Run ()
{
lock (thisLock)
{
while (lNum <100)
{
lNum ++;
Console.WriteLine ("Thread {0}: lNum = {1}", Thread.CurrentThread.Name, lNum);
}
}
}
}

/ * -----------------------------------------------
Output:
Thread0: lNum = 1
Thread0: lNum = 2
Thread0: lNum = 3
Thread0: lNum = 4
Thread0: lNum = 5
Thread0: lNum = 6
Thread0: lNum = 7
Thread0: lNum = 8
Thread0: lNum = 9
Thread0: lNum = 10
Thread0: lNum = 11
Thread0: lNum = 12
Thread0: lNum = 13
Thread0: lNum = 14
Thread0: lNum = 15
Thread0: lNum = 16
Thread0: lNum = 17
Thread0: lNum = 18
Thread0: lNum = 19
Thread0: lNum = 20
Thread0: lNum = 21
Thread0: lNum = 22
Thread0: lNum = 23
Thread0: lNum = 24
Thread0: lNum = 25
Thread0: lNum = 26
Thread0: lNum = 27
Thread0: lNum = 28
Thread0: lNum = 29
Thread0: lNum = 30
Thread0: lNum = 31
Thread0: lNum = 32
Thread0: lNum = 33
Thread0: lNum = 34
Thread0: lNum = 35
Thread0: lNum = 36
Thread0: lNum = 37
Thread0: lNum = 38
Thread0: lNum = 39
Thread0: lNum = 40
Thread0: lNum = 41
Thread0: lNum = 42
Thread0: lNum = 43
Thread0: lNum = 44
Thread0: lNum = 45
Thread0: lNum = 46
Thread0: lNum = 47
Thread0: lNum = 48
Thread0: lNum = 49
Thread0: lNum = 50
Thread0: lNum = 51
Thread0: lNum = 52
Thread0: lNum = 53
Thread0: lNum = 54
Thread0: lNum = 55
Thread0: lNum = 56
Thread0: lNum = 57
Thread0: lNum = 58
Thread0: lNum = 59
Thread0: lNum = 60
Thread0: lNum = 61
Thread0: lNum = 62
Thread0: lNum = 63
Thread0: lNum = 64
Thread0: lNum = 65
Thread0: lNum = 66
Thread0: lNum = 67
Thread0: lNum = 68
Thread0: lNum = 69
Thread0: lNum = 70
Thread0: lNum = 71
Thread0: lNum = 72
Thread0: lNum = 73
Thread0: lNum = 74
Thread0: lNum = 75
Thread0: lNum = 76
Thread0: lNum = 77
Thread0: lNum = 78
Thread0: lNum = 79
Thread0: lNum = 80
Thread0: lNum = 81
Thread0: lNum = 82
Thread0: lNum = 83
Thread0: lNum = 84
Thread0: lNum = 85
Thread0: lNum = 86
Thread0: lNum = 87
Thread0: lNum = 88
Thread0: lNum = 89
Thread0: lNum = 90
Thread0: lNum = 91
Thread0: lNum = 92
Thread0: lNum = 93
Thread0: lNum = 94
Thread0: lNum = 95
Thread0: lNum = 96
Thread0: lNum = 97
Thread0: lNum = 98
Thread0: lNum = 99
Thread0: lNum = 100
* /
Reply

Use magic Report

1

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-2-10 23:15:02
| Show all posts
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
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 Invalid IP Address

Post time: 2020-2-11 10:45:01
| Show all posts
wrong!

If you put code in a lock block, that code can only be executed by one thread at a time. The code can't talk about write operations.
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-2-19 08:15:01
| Show all posts
Sorry, just came today ..

The reason is a synchronization problem ..

Not quite sure how to deal with it in VB, there are many ways in C # ...

Mutex is more commonly used ..
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-19 20:30:01
| Show all posts
chk1360
Support this statement
The landlord's credibility is so low!
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-19 23:45:01
| Show all posts
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 passage also shows that the landlord is confused and has unclear speech.
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-2-21 08:00:01
| Show all posts
Maybe the examples are not very good, but they can also explain some problems:

static int count = 0;
        static void Main (string [] args)
        {
            const int MaxThread = 7;
            Thread [] arrt = new Thread [7];
            for (int i = 0; i <MaxThread; i ++)
            {
                arrt [i] = new Thread (new ThreadStart (threadProc));
                arrt [i] .Name = i.ToString ();
                arrt [i] .Start ();
            }
            Console.Read ();
        }
        static Mutex mx = new Mutex ();
        static void threadProc ()
        {
            while (count <= 100)
            {
                mx.WaitOne ();

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

                mx.ReleaseMutex ();
            }
        }
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