简体   繁体   中英

SIGABRT in critical section simulation C++ code

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?

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

t1.join();
t2.join();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM