开发者

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 t开发者_高级运维hat. If "main" is also a thread, why doesn't it continue running? Is it about POSIX threading?

Thank you.


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


It stops because you call pthread_join and the thread you are joining "to" has an infinite loop.

From that link:

The pthread_join() function suspends execution of the calling thread until the target thread terminates, unless the target thread has already terminated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜