Mark
Mark

Reputation: 18204

C++ Confused about Threads

Basicly this is what I have:

Server::
Server (int port) {
    cout << "Initializing server.\n";

    (...)       

    pthread_t newthread;
    pthread_create(&newthread, NULL, &Server::_startListening, NULL);

    cout << "Exit\n";
    pthread_exit(NULL); // <-- Question
}

void* Server::_startListening (void* param) {
cout << "Start listening for clients ...\n";
return 0;
}

Question: If I don't put pthread_exit(NULL); in the code, it will work when I compile it on Linux (Ubuntu) but it won't work on Mac OSX 10.6.2. When I compile and run it on linux it will say Initializing server, Start listening for clients, Exit while on Mac OSX it will say Initializing for server, Exit, Start listening for clients.

The problem seems to occur around the pthread_exit, if I place it above the cout << Exit. That message will never be displayed (how weird is that).

Am I doing something wrong?

Upvotes: 1

Views: 250

Answers (6)

Scott Stafford
Scott Stafford

Reputation: 44776

I think your question has already been answered above, but you may also want to look into the Boost thread library which will give you easier cross-platforming should you ever go to Windows, plus a nice object-oriented interface.

Upvotes: 0

Potatoswatter
Potatoswatter

Reputation: 137810

pthread_exit is supposed to be called from the thread you want to stop. Calling it from the main() thread will terminate the main thread, and your program will run until the listener threads have exited.

Recommend you read the manpages on pthread_exit and pthread_join.

Upvotes: 0

Eric Mickelsen
Eric Mickelsen

Reputation: 10377

The ordering of your output is indeterminate. That's what concurrency means! The other two answers are right: pthread_exit before your output kills the main thread and you probably intend to join before the main exit occurs.

Upvotes: 1

Tanzelax
Tanzelax

Reputation: 5714

Threading (scheduling, etc) is OS dependent. It looks like on Linux, your OS switches context to your new thread before continuing in the main thread, while on Mac OS, it will continue executing the current thread before switching context. Because this is OS dependent, unless you do some kind of synchronization, there's no reliable way of telling which line of two threads will execute first, and even your tested 'linux will switch contexts' results are unreliable.

pthread_exit, as mentioned above, exits the active current thread (ie: main()), not any other threads (_startListning). You're probably looking for joining (pthread_join) the other thread, not exiting the current.

Upvotes: 0

Anycorn
Anycorn

Reputation: 51465

you probably intend to use pthread_join rather than exit.

Upvotes: 7

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

The problem seems to occur around the pthread_exit, if I place it above the cout << Exit. That message will never be displayed (how weird is that).

Not weird at all. When you call pthread_exit, the current thread stop executing. Since it's only in that thread that you are going to display "exit", once that thread is gone there is nothing left to print it.

In regards to the different order of the print of "exit" and "start listening". Each of those are being printed by a separate thread. Because you have no synchronization, either can be printed first.

Upvotes: 3

Related Questions