views:

99

answers:

4

Hey - I'm having an odd problem with a little toy program I've written, to try out threads.
This is my code:

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

using std::cout;
using std::endl;

void *threadFunc(void *arg) {
    cout << "I am a thread. Hear me roar." << endl;

    pthread_exit(NULL);
}

int main() {
    cout << "Hello there." << endl;
    int returnValue;
    pthread_t myThread;

    returnValue = pthread_create(&myThread, NULL, threadFunc, NULL);

    if (returnValue != 0) {
        cout << "Couldn't create thread! Whoops." << endl;
        return -1;
    }

    return 0;
}

With the first cout in main not commented out, the thread prints fine.
However, without it, the thread doesn't print anything at all.

Any help?

A: 

The fact that it works in one case is pure luck. You have a timing issue, which is your program is exiting before your thread does it's work.

When main() exits all your threads will die. You need some sort of waiting in place to give the other thread time to run before main() exits. Try adding a cin in main() after you create the worker thread and see what happens.

Ultimately you'll want some sort of run-loop and messaging/eventing to communicate between threads.

-jeff

jeffamaphone
A: 

Try adding a pthread_exit(NULL); at the end of main, before the return 0. I suspect that the problem is your main returns and the process exits before the thread has a chance to print. Printing from main might be somehow modifying the timing such that the thread does get a chance to print -- but that's is just sheer luck. You need to ensure that main doesn't return until all of your threads are finished doing their work. Calling pthread_exit from main allows other threads to continue running until they too have called pthread_exit.

Dan Moulding
+5  A: 

Try this:

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

using std::cout;
using std::endl;

void *threadFunc(void *arg) {
    cout << "I am a thread. Hear me roar." << endl;

    pthread_exit(NULL);
}

int main() {
    //cout << "Hello there." << endl;
    int returnValue;
    pthread_t myThread;

    returnValue = pthread_create(&myThread, NULL, threadFunc, NULL);

    if (returnValue != 0) {
        cout << "Couldn't create thread! Whoops." << endl;
        return -1;
    }

    pthread_join( myThread, NULL);

    return 0;
}

The difference between my code and yours is one line - pthread join. This suspends the main thread until the sub-thread has had chance to complete its actions.

In your code, execution reaches the first cout and it's processed. Then, you split off another thread and the main thread carries on until the end, which may or may not be reached before the secondary thread is tidied up. That's where the odd behaviour comes in - what you are experiencing is the case where the main program finishes before the sub-thread has had a chance to, so the program has "returned" and the whole lot is cleaned up by the kernel.

Ninefingers
Perfect! Thanks.
Ink-Jet
+2  A: 

It's a race condition that allows the program to work when the main loop takes a little while to run. Your program is exiting before the thread even has a chance to run.

You should wait for the thread to complete (see pthread_join) before returning from main().

Dave Bacher