CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2004
    Posts
    57

    Simple question: reading / writing a variable in multithreading

    Hi guys. I've a too simple question.

    I have an int variable which is accesed by two threads. One simply increases it at certain times, the other simply reads it's value.

    Code:
    // thread 1
    m_MyVar++;
    
    //thread 2
    if(m_MyVar < MyOtherVar)
        ...
    else
        ...
    Should I implement a CCriticalSection or it's unnecessary??? I'm not sure what can happen when reading a variable whose value is being incremented, or perhaps this is an atomic affair?

    Thanks in advance.

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Simple question: reading / writing a variable in multithreading

    Correct.

    Kuphryn

  3. #3
    Join Date
    May 2003
    Posts
    35

    Re: Simple question: reading / writing a variable in multithreading

    using this function InterLockedExchangeadd (variable, interval)
    For example :

    InterLockedExchangeadd (im_Counter ,1 );

  4. #4
    Join Date
    Apr 2004
    Posts
    57

    Re: Simple question: reading / writing a variable in multithreading

    Can't imagine how useful that's going to be!

    Much thanks guys, you're great!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured