I'm now studying multi-thread programming
However, I have a question about while reading Computer Systems: Programmers' Perspective whose author is Bryant
the badcnt.c is written as
#include "csapp.h"
void *thread(void *vargp);
volatile long cnt = 0;
int main(int argc, char **argv){
long niters;
pthread_t tid1, tid2;
niters = atoi(argv[1]);
Pthread_create(&tid1, NULL, thread, &niters);
Pthread_create(&tid2, NULL, thread, &niters);
Pthread_join(tid1, NULL);
Pthread_join(tid2, NULL);
printf("cnt = %ld\n", cnt);
}
void *thread(void *vargp){
long i;
long niters = *((long *) vargp);
for(i = 0; i < niters; i++) cnt++;
return NULL;
}
I know that in uniprocessor, the machine instructions are completed one after the other in some order, so the cnt result can be from 10 to 20.
My question is
Does cnt result's range can be different from 10 to 20 in dual-core?
In simple thought, I don't think so, but I want to hear many people's opinion.
Thanks