CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2019
    Posts
    72

    threads and function call

    if got an Add function in a shared header:
    Code:
    int Add(int a, int b)
    {
        return a+b;
    }
    is it safe if multiple threads calling the Add function?. if not how to make it safe to avoid unexpected results?.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: threads and function call

    What do you mean by a 'shared header'? a and b are passed by value and the return is also by value - so there's no side-effect from calling the function. So the function itself is thread-safe. But it may depend upon how it's called and how it's return value is used.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Aug 2019
    Posts
    72

    Re: threads and function call

    Got multiple threads each with an instance of a class accessing a function Add. In what cases that kind of a function would be unsafe if accesses by multiple threads?. examples?.. I got to do more C++ digging.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: threads and function call

    I don't use C++ multi-threading - we used Windows threading prior to threads being introduced into C++ and have stuck with this since.

    However, with multi-threading you have to make sure that multiple threads cannot simultaneously access the same data if that data could be changed by another thread. Normally data access is not atomic. ie ++a or a +=2 operations are not 1 operation but internally several. Whilst the statement is being executed, no other thread should have access to a variable if a is used across more than 1 thread - so that the internal operations are not interrupted by a change of thread or with multi-processor systems now common by another thread running on another processor. If a isn't accessed simultaneously (or can't be) then there is no problem. If it is then the accesses need to be done either atomically or by a lock guard so that the operation can only be carried out 1 at the same time. Otherwise you get what is called race conditions.

    Whether this applies to the code in question depends upon the code. With multi-threading you can have what's called 'lock free' code where locks aren't needed as lock code slows performance as access to the variable/code controlled by the lock is then done only 1 at a time irrespective of how many threads are used - slowing access and hence performance.

    Correctly coding multi-threaded code that uses the same variable(s) is not simple. There is a lot of on-line info about this.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Feb 2017
    Posts
    677

    Re: threads and function call

    Quote Originally Posted by @EE@ View Post
    is it safe if multiple threads calling the Add function?.
    The Add function itself is thread-safe in the sense that it is reentrant in the right way,

    https://en.wikipedia.org/wiki/Reentrancy_(computing)

    It means you can call it pretty much as you like in a concurrent environment and it will return the correct value. On the other hand, as 2kaud indicates, you may have to protect the input and output values, respectively, before and after the call.
    Last edited by wolle; October 8th, 2021 at 11:59 AM.

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