Mysterious Negative Counter Value

后端 未结 2 1403
小鲜肉
小鲜肉 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);
    }
    

提交回复
热议问题