Mar*_*ark 1 c++ multithreading g++
基本上这就是我所拥有的:
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;
}
Run Code Online (Sandbox Code Playgroud)
问题:如果我没有把pthread_exit(NULL); 在代码中,它将在我在Linux(Ubuntu)上编译它时工作,但它不适用于Mac OSX 10.6.2.当我在linux上编译并运行它时,它会说"初始化服务器","开始侦听客户端","退出",而在Mac OSX上则会显示"为服务器初始化","退出","开始侦听客户端".
问题似乎发生在pthread_exit周围,如果我把它放在cout << Exit上面.永远不会显示该消息(这有多奇怪).
难道我做错了什么?