Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

main doesn't continue after pthread

Im using Ubuntu 10.10, Code::Blocks with GCC 4.2.

I have written a code like that:

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

using namespace std;

void *thread1proc(void* param){
    while(true)
    cout << "1";

    return 0;
}

int main(){
    pthread_t thread1;

    pthread_create(&thread1,NULL,thread1proc,NULL);
    pthread_join(thread1,NULL);

    cout << "hello";
}

Main starts, creates the thread. But what is weird (for me) is main doesn't continue running. I expect to see "hello" message on screen and end of the program. Because in Windows, in Delphi it worked for me like that. If "main" is also a thread, why doesn't it continue running? Is it about POSIX threading?

Thank you.

like image 204
tcak Avatar asked Dec 21 '22 20:12

tcak


1 Answers

pthread_join will block until thread1 completes (calling pthread_exit or returning), which (as it has an infinite loop) it never will do.

like image 86
Reese Moore Avatar answered Dec 26 '22 12:12

Reese Moore