CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2007
    Posts
    113

    Is it safe to locally use arrays, vectors and calloc when multi-threading

    Hi everyone,

    I have a question about working with threads.
    Is it safe to work with locally assigned memory, such as arrays, vectors and memory temporarily created with calloc (or a simillar alloc operation)?
    For example, in the code below, there's three such methods.
    Is it possible for two threads to exactly simultaneously request memory, which in turn creates a conflict?

    Thank you in advance.


    Code:
    int main()
    {
        std::vector<std::thread *> threads(4);
        for (int i = 0; i < 4; i++)threads(i) = new std::thread(&MyFunction, this);
    
        for (int i = 0; i < 4; i++)
        {
             threads[i]->join();
             delete threads[i];
        }
    }
    
    void MyFunction()
    {
        // METHOD 1: LOCAL ARRAY
        int values1[4] = {1, 2, 3, 4}
    
        // METHOD 2: LOCAL VECTOR
        std::vector<int> values2;
        for (int i = 0; i < 4; i++)values2.push_back(i + 5);
    
        // METHOD 3: CALLOC
        int * values3 = NULL;
        values3 = (int *)calloc(4, sizeof(int));
        for (int i = 0; i < 4; i++)values3[i] = i + 10;
    
        // PERFORM OPERATIONS
    
       // RELEASE THE MEMORY FROM METHOD 3
        free(values3);
    }

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

    Re: Is it safe to locally use arrays, vectors and calloc when multi-threading

    is it possible for two threads to exactly simultaneously request memory,
    Yes.

    Although in C++ programs you should use new/delete rather than calloc/malloc/free
    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++20 Compiler: Microsoft VS2022 (17.0.5)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,121

    Re: Is it safe to locally use arrays, vectors and calloc when multi-threading

    Quote Originally Posted by rmirani View Post
    Is it safe to work ... with calloc (or a simillar alloc operation)?
    I agree with 2kaud.
    You schuld leave the using calloc (or a simillar alloc operation) in c-code.
    In C++ you have to use new/delete. Better - some already implemented (std) container classes.
    Victor Nijegorodov

  4. #4
    Join Date
    Feb 2017
    Posts
    640

    Re: Is it safe to locally use arrays, vectors and calloc when multi-threading

    I agree with the previous posters, at least if you use C++ version 11 or later.

    C++ 11 was a game-changer. Especially concurrency got a much-needed overhaul. Why should you not be able to use standard data structures or allocate memory using calloc simultaneously in several threads without worrying about a system crash? Well, you do not have to, not after C++ 11.

    Furthermore, C++ version 20 offers so much concurrency that it is seldom necessary to manage threads explicitly to exploit parallelisms. Most algorithms in the C++ standard library can run in parallel.
    Last edited by wolle; November 12th, 2021 at 03:02 PM.

  5. #5
    Join Date
    Sep 2007
    Posts
    113

    Re: Is it safe to locally use arrays, vectors and calloc when multi-threading

    Ok, thank you.
    I'm indeed using new/delete rather than calloc/malloc/free, but I forgot to explicitly ask for new/delete in the list of possibilities.
    As for the C++ version, I'm using Microsoft Visual Studio 2017, which apparently uses C++ 14.16, so I assume that this should be sufficient?
    I've never had any issues with the code itself, but it occurred to me today that I should just double-check to see if perhaps there's some hidden bugs.

  6. #6
    Join Date
    Feb 2017
    Posts
    640

    Re: Is it safe to locally use arrays, vectors and calloc when multi-threading

    Quote Originally Posted by rmirani View Post
    Ok, thank you.
    I'm indeed using new/delete rather than calloc/malloc/free, but I forgot to explicitly ask for new/delete in the list of possibilities.
    As for the C++ version, I'm using Microsoft Visual Studio 2017, which apparently uses C++ 14.16, so I assume that this should be sufficient?
    I've never had any issues with the code itself, but it occurred to me today that I should just double-check to see if perhaps there's some hidden bugs.
    I understand there may be company restrictions. But if you are free to choose, I recommend you update to the latest Visual Studio version and the latest C++ standard. With a reasonable delay, of course, to let things settle in.

    I also recommend not using C++ as just a better C. Instead, use C++ as better than its peers, such as Java, C#, Python and Rust, etcetera. And this means preferring the modern features of C++ over its C legacy part.
    Last edited by wolle; November 13th, 2021 at 03:14 AM.

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

    Re: Is it safe to locally use arrays, vectors and calloc when multi-threading

    I'm using Microsoft Visual Studio 2017, which apparently uses C++ 14.16
    The current version is VS 2022 which provides support for C++20. The Community version is still free. I suggest you install VS2022. This will happily co-exist with VS2017. It uses the same installer.
    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++20 Compiler: Microsoft VS2022 (17.0.5)

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