0

I am new to threads, I am trying to simulate the critical section race condition problem in this code.

#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::chrono;

int x = 20;

void increment()
{
    ++x;
}

void decrement()
{
    --x;
}

int main()
{
    thread t1(increment);
    thread t2(decrement);
    cout << x;
    return 0;
}

But, this code terminates with SIGABRT.
terminate called without an active exception
21
Why I am getting SIGABRT in this code?

  • 1
    Does this answer your question? [C++ terminate called without an active exception](https://stackoverflow.com/questions/7381757/c-terminate-called-without-an-active-exception) – Nate Eldredge Mar 16 '21 at 06:13

1 Answers1

0

You must call join for the threads so that they are properly terminated

t1.join();
t2.join();
Yitshak Yarom
  • 84
  • 1
  • 4