“A reference to a volatile field will not be treated as volatile” implications

前端 未结 4 1432
慢半拍i
慢半拍i 2020-12-02 15:52

The following code

using System.Threading;

class Test
{
    volatile int counter = 0;
    public void Increment()
    {
        Interlocked.Increment(ref co         


        
4条回答
  •  不知归路
    2020-12-02 16:10

    Use this:

            #pragma warning disable 420
            //                       M
            //                      dM
            //                      MMr
            //                     4MMML                  .
            //                     MMMMM.                xf
            //     .              "MMMMM               .MM-
            //      Mh..          +MMMMMM            .MMMM
            //      .MMM.         .MMMMML.          MMMMMh
            //       )MMMh.        MMMMMM         MMMMMMM
            //        3MMMMx.     'MMMMMMf      xnMMMMMM"
            //        '*MMMMM      MMMMMM.     nMMMMMMP"
            //          *MMMMMx    "MMMMM\    .MMMMMMM=
            //           *MMMMMh   "MMMMM"   JMMMMMMP
            //             MMMMMM   3MMMM.  dMMMMMM            .
            //              MMMMMM  "MMMM  .MMMMM(        .nnMP"
            //  =..          *MMMMx  MMM"  dMMMM"    .nnMMMMM*
            //    "MMn...     'MMMMr 'MM   MMM"   .nMMMMMMM*"
            //     "4MMMMnn..   *MMM  MM  MMP"  .dMMMMMMM""
            //       ^MMMMMMMMx.  *ML "M .M*  .MMMMMM**"
            //          *PMMMMMMhn. *x > M  .MMMM**""
            //             ""**MMMMhx/.h/ .=*"
            //                      .3P"%....
            //                    nP"     "*MMnx
            if(Interlocked.CompareExchange(ref isLoaded, 1, 0) != 0)
                return;
            #pragma warning restore 420
    

提交回复
热议问题