#include <stdio.h>
#include <pthread.h>
#define NUM_THREADS 4
int shared = 0;
void *foo()
{
for (int i = 0; i < 10000; ++i) {
shared += 1;
}
return NULL;
}
int main()
{
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; ++i) {
pthread_create(&threads[i], NULL, foo, NULL);
}
for (int i = 0; i < NUM_THREADS; ++i) {
pthread_join(threads[i], NULL);
}
printf("final result is %d\n", shared
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxwdGhyZWFkLmg+CgojZGVmaW5lIE5VTV9USFJFQURTIDQKCmludCBzaGFyZWQgPSAwOwoKdm9pZCAqZm9vKCkKewogICAgZm9yIChpbnQgaSA9IDA7IGkgPCAxMDAwMDsgKytpKSB7CiAgICAgICAgc2hhcmVkICs9IDE7CiAgICB9CiAgICByZXR1cm4gTlVMTDsKfQoKaW50IG1haW4oKQp7CiAgICBwdGhyZWFkX3QgdGhyZWFkc1tOVU1fVEhSRUFEU107CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBOVU1fVEhSRUFEUzsgKytpKSB7CiAgICAgICAgcHRocmVhZF9jcmVhdGUoJnRocmVhZHNbaV0sIE5VTEwsIGZvbywgTlVMTCk7CiAgICB9CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBOVU1fVEhSRUFEUzsgKytpKSB7CiAgICAgICAgcHRocmVhZF9qb2luKHRocmVhZHNbaV0sIE5VTEwpOwogICAgfQoKICAgIHByaW50ZigiZmluYWwgcmVzdWx0IGlzICVkXG4iLCBzaGFyZWQpOwoKICAgIHJldHVybiAwOwp9