简体   繁体   English

C ++对线程感到困惑

[英]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); 问题:如果我不放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. 在代码中,当我在Linux(Ubuntu)上编译它时,它将起作用,但在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. 当我在linux上编译并运行它时,它将说“正在初始化服务器,开始侦听客户端,退出”,而在Mac OSX上,它将说“正在初始化服务器,退出,开始侦听客户端”。

The problem seems to occur around the pthread_exit, if I place it above the cout << Exit. 如果我将其放置在cout << Exit上方,则问题似乎出现在pthread_exit周围。 That message will never be displayed (how weird is that). 该消息将永远不会显示(这有多怪异)。

Am I doing something wrong? 难道我做错了什么?

您可能打算使用pthread_join而不是退出。

The problem seems to occur around the pthread_exit, if I place it above the cout << Exit. 如果我将其放置在cout << Exit上方,则问题似乎出现在pthread_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. 调用pthread_exit时,当前线程停止执行。 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. 因为没有同步,所以任何一个都可以先打印。

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. 另外两个答案是正确的:pthread_exit在输出终止主线程之前,并且您可能打算在主出口出现之前加入。

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. 看起来在Linux上,您的OS在继续进入主线程之前将上下文切换到新线程,而在Mac OS上,它将在切换上下文之前继续执行当前线程。 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. 因为这是依赖于OS的,否则除非进行某种同步,否则就没有可靠的方法来告诉两个线程中的哪一行将首先执行,甚至经过测试的“ Linux将切换上下文”结果也不可靠。

pthread_exit, as mentioned above, exits the active current thread (ie: main()), not any other threads (_startListning). 如上所述,pthread_exit退出当前活动线程(即:main()),而不退出其他任何线程(_startListning)。 You're probably looking for joining (pthread_join) the other thread, not exiting the current. 您可能正在寻找加入(pthread_join)另一个线程,而不退出当前线程。

pthread_exit is supposed to be called from the thread you want to stop. 应该从您要停止的线程中调用pthread_exit Calling it from the main() thread will terminate the main thread, and your program will run until the listener threads have exited. main()线程调用它将终止主线程,并且您的程序将运行直到侦听器线程退出。

Recommend you read the manpages on pthread_exit and pthread_join . 建议您阅读pthread_exitpthread_join的联机帮助页。

我认为您的问题已经在上面回答了,但是您可能还希望研究Boost线程库,如果您使用Windows,它会为您提供更轻松的跨平台支持,以及一个不错的面向对象的界面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM