I'm using C and I want to get the String "ABCABCABCABCABCABC" in the output screen through multithreading, one thread does the 'A' character, another 'B' and the third one 'C'. If I compile the following code
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#define cantidad_impresion_letra 6
pthread_mutex_t semaforo = PTHREAD_MUTEX_INITIALIZER;
void *escribirA(void *unused){
int i;
for(i=0;i<cantidad_impresion_letra;i++){
pthread_mutex_lock(&semaforo);
printf("A");
pthread_mutex_unlock(&semaforo);
}
}
void *escribirB(void *unused){
int i;
for(i=0;i<cantidad_impresion_letra;i++){
pthread_mutex_lock(&semaforo);
printf("B");
pthread_mutex_unlock(&semaforo);
}
}
void *escribirC(void *unused){
int i;
for(i=0;i<cantidad_impresion_letra;i++){
pthread_mutex_lock(&semaforo);
printf("C");
pthread_mutex_unlock(&semaforo);
}
}
int main(){
pthread_t thread1, thread2, thread3;
pthread_create(&thread1,NULL,escribirA,NULL);
pthread_create(&thread2,NULL,escribirB,NULL);
pthread_create(&thread3,NULL,escribirC,NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
return(0);
}
on Windows through Dev-C++, the console throws me: ABACBACBACBACBACBC but if I compile THE SAME code in Linux, I get this: CCCCCCBBBBBBAAAAAA can someone explain me this pls? Btw, sorry for the uga-buga english