Anamort
Anamort

Reputation: 341

Different results in multithread code when I use printf()

I'm working on a multithread C Programming project and before I begin, I have written a code for practice. Although it is written in multithread, result is like sequential. However, if I used printf, result is like multithread. Here is the code:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>

pthread_mutex_t myMutex;
int myInt[100];
int i=0;

void *PrintThread1()
{
    printf("this is initial of FIRST Thread\n");
    for (int j=0; j<33; j++) {
        pthread_mutex_lock(&myMutex);
        //printf("1\n");
        myInt[i]=1;
        i++;
        pthread_mutex_unlock(&myMutex);
    }
    pthread_exit(NULL);

}

void *PrintThread2()
{
    printf("this is initial of SECOND Thread\n");
    for (int j=0; j<33; j++) {
        pthread_mutex_lock(&myMutex);
        //printf("2\n");
        myInt[i]=2;
        i++;
        pthread_mutex_unlock(&myMutex);
    }

    pthread_exit(NULL);

}

void *PrintThread3()
{
    printf("this is initial of THIRD Thread\n");
    for (int j=0; j<33; j++) {
        pthread_mutex_lock(&myMutex);
        //printf("3\n");
        myInt[i]=3;
        i++;
        pthread_mutex_unlock(&myMutex);
    }

    pthread_exit(NULL);
}



int main(int argc, char *argv[])
{
    pthread_t firstThread, secondThread, thirdThread;
    //pthread_attr_t attr;
    pthread_mutex_init(&myMutex, NULL);
    //pthread_attr_init(&attr);
    //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    int ft;
    ft = pthread_create(&firstThread, NULL, PrintThread1, NULL);
    if (ft){
        printf("ERROR; return code from pthread_create() is %d\n", ft);
        exit(-1);
    }
    ft = pthread_create(&secondThread, NULL, PrintThread2, NULL);
    if (ft){
        printf("ERROR; return code from pthread_create() is %d\n", ft);
        exit(-1);
    }
    ft = pthread_create(&thirdThread, NULL, PrintThread3, NULL);
    if (ft){
        printf("ERROR; return code from pthread_create() is %d\n", ft);
        exit(-1);
    }

    pthread_join(firstThread, NULL);
    pthread_join(secondThread, NULL);
    pthread_join(thirdThread, NULL);

    pthread_mutex_destroy(&myMutex);

    for (int j=0;j<100; j++) {
        printf("myInt[%d] = %d\n",j,myInt[j]);
    }
    printf("\n");

    //pthread_exit(NULL);
    return 0;
}

My result is:

this is initial of SECOND Thread
this is initial of FIRST Thread
this is initial of THIRD Thread
myInt[0] = 2
myInt[1] = 2
myInt[2] = 2
myInt[3] = 2
myInt[4] = 2
myInt[5] = 2
myInt[6] = 2
myInt[7] = 2
myInt[8] = 2
myInt[9] = 2
myInt[10] = 2
myInt[11] = 2
myInt[12] = 2
myInt[13] = 2
myInt[14] = 2
myInt[15] = 2
myInt[16] = 2
myInt[17] = 2
myInt[18] = 2
myInt[19] = 2
myInt[20] = 2
myInt[21] = 2
myInt[22] = 2
myInt[23] = 2
myInt[24] = 2
myInt[25] = 2
myInt[26] = 2
myInt[27] = 2
myInt[28] = 2
myInt[29] = 2
myInt[30] = 2
myInt[31] = 2
myInt[32] = 2
myInt[33] = 1
myInt[34] = 1
myInt[35] = 1
myInt[36] = 1
myInt[37] = 1
myInt[38] = 1
myInt[39] = 1
myInt[40] = 1
myInt[41] = 1
myInt[42] = 1
myInt[43] = 1
myInt[44] = 1
myInt[45] = 1
myInt[46] = 1
myInt[47] = 1
myInt[48] = 1
myInt[49] = 1
myInt[50] = 1
myInt[51] = 1
myInt[52] = 1
myInt[53] = 1
myInt[54] = 1
myInt[55] = 1
myInt[56] = 1
myInt[57] = 1
myInt[58] = 1
myInt[59] = 1
myInt[60] = 1
myInt[61] = 1
myInt[62] = 1
myInt[63] = 1
myInt[64] = 1
myInt[65] = 1
myInt[66] = 3
myInt[67] = 3
myInt[68] = 3
myInt[69] = 3
myInt[70] = 3
myInt[71] = 3
myInt[72] = 3
myInt[73] = 3
myInt[74] = 3
myInt[75] = 3
myInt[76] = 3
myInt[77] = 3
myInt[78] = 3
myInt[79] = 3
myInt[80] = 3
myInt[81] = 3
myInt[82] = 3
myInt[83] = 3
myInt[84] = 3
myInt[85] = 3
myInt[86] = 3
myInt[87] = 3
myInt[88] = 3
myInt[89] = 3
myInt[90] = 3
myInt[91] = 3
myInt[92] = 3
myInt[93] = 3
myInt[94] = 3
myInt[95] = 3
myInt[96] = 3
myInt[97] = 3
myInt[98] = 3
myInt[99] = 0

Program ended with exit code: 0

However, if I used printf in the mutex which is commented in code, the result is:

this is initial of FIRST Thread
this is initial of THIRD Thread
this is initial of SECOND Thread
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
1
3
2
myInt[0] = 1
myInt[1] = 3
myInt[2] = 2
myInt[3] = 1
myInt[4] = 3
myInt[5] = 2
myInt[6] = 1
myInt[7] = 3
myInt[8] = 2
myInt[9] = 1
myInt[10] = 3
myInt[11] = 2
myInt[12] = 1
myInt[13] = 3
myInt[14] = 2
myInt[15] = 1
myInt[16] = 3
myInt[17] = 2
myInt[18] = 1
myInt[19] = 3
myInt[20] = 2
myInt[21] = 1
myInt[22] = 3
myInt[23] = 2
myInt[24] = 1
myInt[25] = 3
myInt[26] = 2
myInt[27] = 1
myInt[28] = 3
myInt[29] = 2
myInt[30] = 1
myInt[31] = 3
myInt[32] = 2
myInt[33] = 1
myInt[34] = 3
myInt[35] = 2
myInt[36] = 1
myInt[37] = 3
myInt[38] = 2
myInt[39] = 1
myInt[40] = 3
myInt[41] = 2
myInt[42] = 1
myInt[43] = 3
myInt[44] = 2
myInt[45] = 1
myInt[46] = 3
myInt[47] = 2
myInt[48] = 1
myInt[49] = 3
myInt[50] = 2
myInt[51] = 1
myInt[52] = 3
myInt[53] = 2
myInt[54] = 1
myInt[55] = 3
myInt[56] = 2
myInt[57] = 1
myInt[58] = 3
myInt[59] = 2
myInt[60] = 1
myInt[61] = 3
myInt[62] = 2
myInt[63] = 1
myInt[64] = 3
myInt[65] = 2
myInt[66] = 1
myInt[67] = 3
myInt[68] = 2
myInt[69] = 1
myInt[70] = 3
myInt[71] = 2
myInt[72] = 1
myInt[73] = 3
myInt[74] = 2
myInt[75] = 1
myInt[76] = 3
myInt[77] = 2
myInt[78] = 1
myInt[79] = 3
myInt[80] = 2
myInt[81] = 1
myInt[82] = 3
myInt[83] = 2
myInt[84] = 1
myInt[85] = 3
myInt[86] = 2
myInt[87] = 1
myInt[88] = 3
myInt[89] = 2
myInt[90] = 1
myInt[91] = 3
myInt[92] = 2
myInt[93] = 1
myInt[94] = 3
myInt[95] = 2
myInt[96] = 1
myInt[97] = 3
myInt[98] = 2
myInt[99] = 0

Program ended with exit code: 0

What is the reason of these?

Upvotes: 0

Views: 93

Answers (1)

Max Fomichev
Max Fomichev

Reputation: 244

I think your model is too simple. You have only 2 operation inside the thread loops. There is no enough time to switch contexts from thread#1 to thread#2 to thread#3. Try to add usleep(100) before pthread_mutex_unlock(&myMutex) in each thread loop. Then, I suppose, you will see the expected behaviour.

Upvotes: 1

Related Questions