I'm currently learning about threads and my professor in class showed us this example attempting to demonstrate concurrency for threads
#include <iostream>
using namespace std;
void* threadHandler(void * val) {
    long long* a = (long long *) val;
    for (long long i = 0; i<100000; i++)
        (*a)++;
        
    int id = pthread_self();
    printf("thread : %d a = %d\n", id, *a);
    pthread_exit(nullptr);
}
int main() {
    long long a = 0;
    pthread_t t1, t2;
    pthread_setconcurrency(2);
    pthread_create(&t1, nullptr, threadHandler, &a);
    pthread_create(&t2, nullptr, threadHandler, &a);
    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    printf("main : a = %d\n", a);
    return 0;
}
Could anyone explain why the resulting value of a is not 200,000 as expected? The values are inconsistent, and always below 200,000.
However, when the incrementing loop is reduced to 10,000 , the result is as expected : a = 20,000.
Thanks.


 
    