0

I want to add a series of numbers [1-->5000] with threads. But the result is not correct.

The goal is only to understand the threading well, because I am a beginner.

I tried this:

void thread_function(int i, int (*S))
{
    (*S) = (*S) + i;
}

main()
{
    std::vector<std::thread> vecto_Array;
    int i = 0, Som = 0;

    for(i = 1; i <= 5000; i++)
    {
        vecto_Array.emplace_back([&](){ thread_function(i, &Som); });
    }    

    for(auto& t: vecto_Array)
    {
        t.join();
    }

    std::cout << Som << std::endl;
}

And I tried this:

int thread_function(int i)
{
    return i;  
}

main()
{
    std::vector<std::thread> vecto_Array;
    int i = 0, Som = 0;

    for(i = 1; i <= 5000; i++)
    {
        vecto_Array.emplace_back([&](){ Som = Som + thread_function(i); });
    }    

    for(auto& t: vecto_Array)
    {
        t.join();
    }

    std::cout << Som << std::endl;
}

The result is always wrong. Why?

I solved the problem as follows:

void thread_function(int (*i),int (*S))
{
    (*S)=(*S)+(*i);
    (*i)++;
}
main()
{
    std::vector<std::thread> vecto_Array;
    int i=0,j=0,Som=0;
    for(i=1;i<=5000;i++)
    {
     vecto_Array.emplace_back([&](){thread_function(&j,&Som);});     
    }    
    for(auto& t: vecto_Array)
    {
    t.join();
    }
    std::cout << Som<<std::endl;
}

But is there anyone to explain to me why it did not work when taking "i of loop" ?

  • 1
    Does this answer your question? https://stackoverflow.com/questions/34510/what-is-a-race-condition – 463035818_is_not_an_ai Feb 03 '21 at 17:33
  • You need to serialize the access to `Som` between the threads, such as with a `std::mutex`, so that they don't all try to write to the same memory at the *exact* same time, corrupting the bytes being written. – Remy Lebeau Feb 03 '21 at 17:33
  • I have a try : '#include std::mutex mu;.. mu.lock(); (*S)=(*S)+i; mu.unlock();' – program_lover Feb 03 '21 at 17:36
  • 2
    just a warning: multi-threading isn't something that one can learn by trial and error. Even more than usual it is simple to write code that appears to be fine but isn't. Cant recommend something specifically, but you can try here: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Feb 03 '21 at 17:40
  • "I solved the problem as follows" - no, you didn't *solve* the problem, you managed to produce variant of code which *hides* the problem on your particular platform and configuration. It's by no means correct multi-thread code and would broke again on any non-trivial extension of it. – Ped7g Feb 04 '21 at 14:35
  • what s the solution in your opinion..? – program_lover Feb 04 '21 at 16:44

1 Answers1

0
  1. Your attempt #1 has a race condition. See What is a race condition?
  2. Your Attempt #2 neglects the standard, which says about the thread function these words:

Any return value from the function is ignored.

(see: https://en.cppreference.com/w/cpp/thread/thread/thread )

  1. Your attempt #3 has a race condition.

Concurrent programming is an advanced topic. What you need is a book or tutorial. I first learned it from Bartosz Milewski's course: https://www.youtube.com/watch?v=80ifzK3b8QQ&list=PL1835A90FC78FF8BE&index=1 but be warned that it will likely take years before you become comfortable in concurrency. I am still not. I guess what you need as a beginner is std::async (see Milewski's tutorial or use Google). Even gentler learning curve is with OpenMP https://en.wikipedia.org/wiki/OpenMP , which could be called "parallelization for the masses".

zkoza
  • 2,644
  • 3
  • 16
  • 24