n0o0a0h0
V2EX  ›  C

关于 C++多线程的一个问题

  •  
  •   n0o0a0h0 · Jun 20, 2015 · 3203 views
    This topic created in 4164 days ago, the information mentioned may be changed or developed.

    现在学长让我把一个1300多行的代码改成多线程,%>_<%,然后开始看了一些多线程的指导,链接在此
    有一个例子如下所示:

    #include <iostream>
    #include <thread>
    
    static const int num_threads = 10;
    
    //This function will be called from a thread
    
    void call_from_thread(int tid) {
        std::cout << "Launched by thread " << tid << std::endl;
    }
    
    int main() {
        std::thread t[num_threads];
    
        //Launch a group of threads
        for (int i = 0; i < num_threads; ++i) {
            t[i] = std::thread(call_from_thread, i);
        }
    
        std::cout << "Launched from the main\n";
    
        //Join the threads with the main thread
        for (int i = 0; i < num_threads; ++i) {
            t[i].join();
        }
    
        return 0;
    }
    

    例子说它的输出应该为:

    但是我的输出确实乱掉的:

    我是在xcode里面编译的哦。
    感谢各位指导。如果能指引一下多线程的方向就更感谢啦!

    Supplement 1  ·  Jun 21, 2015
    多谢大家,使用mutex锁住cout即可:
    ```
    static std::mutex mtx;
    ```
    ```
    mtx.lock();
    std::cout << "Launched by thread " << tid << std::endl;
    mtx.unlock();
    ```
    ```
    Launched by thread 4
    Launched by thread 3
    Launched by thread 1
    Launched by thread 5
    Launched by thread 0
    Launched by thread 2
    Launched by thread 6
    Launched by thread 7
    Launched by thread 8
    Launched from the main
    Launched by thread 9
    ```

    又一个问题来了,是否可以控制thread输出的顺序呢?
    Supplement 2  ·  Jun 21, 2015
    (http://www.cplusplus.com/reference/mutex/mutex/lock/)的例子跟我这个差不多,锁住之后就是正常按照顺序输出,晕.
    现在学长要求我讲SVM(支持向量机)一共1500多行放到多thread去执行。里面的算法是搞懂了,但是多个thread执行。感觉是没有什么思路。
    18 replies  •  2015-06-22 10:08:19 +08:00
    aheadlead
    aheadlead
    Jun 20, 2015
    1
    46fo
    46fo
    Jun 20, 2015
    2
    0o0 按理说是一次性输出的
    limhiaoing
    limhiaoing
    Jun 20, 2015
    3
    你看的那篇文章里写了这么一句话
    Actually if you run the above code on your system you can get a completely different result or even some mangled characters.
    limhiaoing
    limhiaoing
    Jun 20, 2015
    4
    我也写过一篇关于C++11 多线程的简单的入门文章
    http://blog.poxiao.me/p/multi-threading-in-cpp11-part-1-thread-and-future/
    涉及到 std::thread std::future std::async std::promise std::packaged_task等
    zonyitoo
    zonyitoo
    Jun 21, 2015
    5
    乱掉很正常,你给的例子里它也是乱掉的,只是没有你的这么乱罢了。
    多线程同时调用cout,没有人能知道它到底是以什么样的顺序进入到cout的buffer里面。
    正确的姿势是用Mutex锁住cout。
    n0o0a0h0
    n0o0a0h0
    OP
    Jun 21, 2015
    6
    @limhiaoing 谢谢 你的blog 写得好详细 我细细品味一下
    @aheadlead
    @zonyitoo
    谢谢两位 我是初学者啦~~~
    @xdeng 可以参考楼上的回答
    46fo
    46fo
    Jun 21, 2015
    7
    原来是c++11 自带的线程
    aheadlead
    aheadlead
    Jun 21, 2015
    8
    @n0o0a0h0 加油!
    secondwtq
    secondwtq
    Jun 21, 2015
    9
    我在 Linux 下测试,结果和原文差不多。
    在 Mac 下测试,结果和楼主给的差不多。
    SoloCompany
    SoloCompany
    Jun 21, 2015
    10
    头像好萌
    chlx
    chlx
    Jun 21, 2015
    11
    即使满足sequential order也是乱掉的。 同楼上, 锁住cout
    middleware
    middleware
    Jun 21, 2015
    12
    iostream 自己有同步。由 sync_with_stdio() 控制。
    shimoyo
    shimoyo
    Jun 21, 2015 via iPhone
    13
    前段时间的操作系统课讲的进程的互斥,老师讲的例子几乎和这个一样……
    inevermore
    inevermore
    Jun 21, 2015
    14
    你们啊,naive,最简单的方式是使用printf,他是默认原子性的。
    n0o0a0h0
    n0o0a0h0
    OP
    Jun 21, 2015
    15
    @chlx 锁定了哦 不知道是否可以控制thread输出的是顺序呢?
    n0o0a0h0
    n0o0a0h0
    OP
    Jun 21, 2015
    16
    @inevermore 其实也是初学这方面 ~~多谢批评
    Axurez
    Axurez
    Jun 22, 2015
    17
    cppreference 的例子说了,Possible output (order of lines may vary, but they are never intermingled)
    并不是顺序的
    n0o0a0h0
    n0o0a0h0
    OP
    Jun 22, 2015
    18
    @Axurez 好的~~~ 谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1876 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 11:47 · PVG 19:47 · LAX 03:47 · JFK 06:47
    ♥ Do have faith in what you're doing.