| |

VerySource

 Forgot password?
 Register
Search
View: 857|Reply: 9

Problems with public static members in C #

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-1 14:40:01
| Show all posts |Read mode
I'm having trouble using public members in C # recently.

It is said that static members are conducive to improving the operating efficiency of the program. Recently, all class members in the bin directory of asp.net in a website with a relatively high load have been written as static. For program speed, I found that using static members is indeed much higher than instantiating one class at a time.

However, in the case of a large number of visitors, the problem occurred and it was found that the operation result of the program was incorrect.
E.g
private static int A;
public static GetA2 (int a)
{
    A = a;
    return A * A;
}
Asp.net is a typical multi-threaded application, when multiple threads execute this set of code at the same time, an error occurs.
Checked out the .net 2.0 SDK, which says to use lock to ensure that when one thread is in a critical section of code, another thread does not enter the critical section.

Anyway, I do n’t understand and ca n’t figure it out. My foundation is poor.

Ask the experts what is the critical section, what should I do about this problem? Do you want to change all class members to instance members?
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-13 23:00:01
| Show all posts
private static int A;
public static int GetA2 (int a)
{
    A = a;
    return A * A;
}
Reply

Use magic Report

0

Threads

7

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-14 21:45:01
| Show all posts
The instance cost of this method should not be large, it is not necessary to use static

You put the field private static int A;
Try removing static
Reply

Use magic Report

0

Threads

26

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-3-14 22:00:01
| Show all posts
A static variable is equivalent to a global variable in a program domain.There is only one instance. When multiple threads operate on such variables, use lock to ensure that only one thread can currently work on it. Other threads will Wait until the static variable is released by the lock.If you do not lock the static variable, multiple threads preempting the static variable may cause an exception.
It's like that Mao Hang is only enough for one person to enter at a time. The user must lock the door to prevent another person from entering before using it. If you don't lock the door, others can't help but have to squeeze you, then the result ... .do not know......
Reply

Use magic Report

0

Threads

31

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-3-15 12:30:01
| Show all posts
The critical section is actually the object monitor
Because multi-threaded operation is out of order
For example: Thread 1 enters the method and runs A = a ;, but has not run return A * A;
The CPU time slice is preempted, another thread enters the operation, and a new value of a is passed in because it is static.
So the change of the A value by the second thread will affect the first thread, causing an error in the return value, and the problem does not occur in the instance

If necessary, synchronize this number, or don't cache A. Unless shared variables, it is best not to use static, synchronization affects performance. There are many synchronization methods, such as lock, mutex, etc. There is only one single variable.
Reply

Use magic Report

0

Threads

27

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-3-16 18:45:01
| Show all posts
There are three ways to lock mutex, lock, and monitor. Among them, lock is the simplest, as follows:
private static int A;
public static int GetA2 (int a)
{
   lock (this)
   {
    A = a;
    return A * A;
   }
}
Reply

Use magic Report

0

Threads

7

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-17 06:45:01
| Show all posts
When the lock is executed, others that need to be executed can only be unlocked. People cannot open multiple pages.
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-18 01:45:01
| Show all posts
Did not understand
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-7-16 20:00:01
| Show all posts
In an asp.net application with a high load (up to 1000 requests per second), is the attribute member of the class static or not as good as the instance? ? ?

For member methods that only use local variables, are they not affected by multithreading? ?
Thank you all.
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-18 08:15:01
| Show all posts
There is a certain inspiration!!!!!!!!!!!!
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