samedi 25 août 2018

Atomics and thread starvation

I have a question regarding both C++ standard and more generally computer architecture. Imagine following piece of code:

#include <atomic>
#include <thread>

std::atomic<int> a {0};

int main () {

    std::thread t1([&a](){
        int i = 1;
        int ao = a.load();
        while(!a.compare_exchange_weak(ao, i));
    }), t2([&a](){
        int i = 1;
        int ao = a.load();
        while(!a.compare_exchange_weak(ao, i));
    });

    t1.join();
    t2.join();
    return 0;
}

I have two questions:

  1. Can this according to standard, in theory lead to thread starvation?
  2. Secondly, can this in practice on x86 architecture lead to starvation? Can I rely on it or no?

Aucun commentaire:

Enregistrer un commentaire