1

Consider the next piece of code :

#include <iostream>
#include <pthread.h>
#include <string.h>

using namespace std;

pthread_t tid[3];

void* printMe(void* arg)
{
    pthread_t id = pthread_self();
    if(pthread_equal(id,tid[0]))
    {
        cout << "first thread's in function" << endl;
    }
    if(pthread_equal(id,tid[1]))
    {
        cout << "second thread's in function" << endl;
    }
    if(pthread_equal(id,tid[2]))
    {
        cout << "third thread's in function" << endl;
    }

}

int main() {

    int i = 0;
    int err;

    while (i < 3)
    {
        err = pthread_create(&(tid[i]), NULL, printMe, NULL);

        if (err != 0)
        {
            cout << "failed to create thread number " << i << strerror(i) << endl;
        }
        else
        {
            cout << "main() : creating thread number " << i << endl;
        }

        i++;
    }

    return 0;
}

I don't understand when does a thread invoke his function? Is that right as it's created? (while, simultaneously, the main thread keeps on creating the other threads?)

Moreover, i don't understand the output -

main() : creating thread number 0
main() : creating thread number 1
main() : creating thread number 2
first thread's in function
first thread's in function

First thread invoked his function twice while none of the other threads invoked theirs.

Then, i compiled again and got -

main() : creating thread number 0
first thread's in function
main() : creating thread number 1
second thread's in function
main() : creating thread number 2

Once again, what about the third thread?

Why "sometimes" threads don't get to invoke their functions?

Alex Goft
  • 1,114
  • 1
  • 11
  • 23

2 Answers2

2

Your main() isn't joining the threads before returning and thereby terminating the program, so it's more or less random whether you see any given thread's output.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
2

Your main function terminates before the run of the thread. You have to wait the end of all threads before returning from the main. See this question : How can I wait for any/all pthreads to complete?

Note : You flag it as C++, any reason to not use threads from C++11 or boost ?

Community
  • 1
  • 1
Caduchon
  • 4,574
  • 4
  • 26
  • 67
  • Thank you! and its because i'm doing a school project in which we're not allowed to use boost. tough life – Alex Goft Apr 20 '16 at 08:32
  • *"any reason to not use threads from boost ?"* - Standard Library threads should be preferred for >= C++11-capable compilers.... – Tony Delroy Apr 20 '16 at 09:02
  • @TonyD : I agree with that, I'm restricted to gcc 4.1.2 at work, then I'm still thinking C++98. – Caduchon Apr 20 '16 at 09:14
  • @Caduchon: with the time I spend getting and rebuilding 3rd party packages someone once built with VS2008 and committed binaries for - so we can finish our move to VS2013 and 64 bit and use C++11 - I'd seriously consider settling for gcc 4.1.2 too. Cheers. – Tony Delroy Apr 20 '16 at 09:42