Mysterious Negative Counter Value

后端 未结 2 1404
小鲜肉
小鲜肉 2021-01-28 02:40

I\'m creating new threads for every sql call for a project. There are millions of sql calls so I\'m calling a procedure in a new thread to handle the sql calls.

In doing

相关标签:
2条回答
  • 2021-01-28 03:16

    Nothing mysterious about it, you are using threading, right?

    The ++ and -- operator aren't thread safe. Do this.

    public static void processline()
    {
        Interlocked.Increment(ref counter);
        sql.ExecuteNonQuery();
        Interlocked.Decrement(ref counter);
    }
    
    0 讨论(0)
  • 2021-01-28 03:32

    How to overcome

    Use Interlocked.Increment and Interlocked.Decrement to safely change the value of the counter.

    Why this happens

    You have counter as variable, which is shared across multiple threads. This variable is non-volatile and not wrapped by any synchronization block, so each thread has its own copy of that variable. So if two threads try to change it value at the same time, value would be overrriden by copy from thread which accessed it last.
    Imagine you start your code in two different threads:

    1. Initially counter equals zero, both threads havy copy of that
    2. Both thread invoke increment their cached copies, and than change counter. So thread1 increments its copy to 1 and overrides counter, thread2 also increments its copy (still equal to zero) to 1 and overrides counter to, again, 1. After that that value is propagated to all threads (all copies are refreshed)
    3. Both threads invoke sql query. Due to variability in sql performance, these queries are completed in different time.
    4. Thread1 ends sql query, decrements counter from 1 to 0. Counter value is propagated to all threads
    5. After some time, Thread2 ends sql query, decrement counter from already propagated 0 to -1. Counter value is propagated to all threads. And it is -1.
    0 讨论(0)
提交回复
热议问题