V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
n0o0a0h0
V2EX  ›  C

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

  •  
  •   n0o0a0h0 · 2015-06-20 22:14:06 +08:00 · 1713 次点击
    这是一个创建于 3235 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在学长让我把一个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里面编译的哦。
    感谢各位指导。如果能指引一下多线程的方向就更感谢啦!

    第 1 条附言  ·  2015-06-21 17:15:55 +08:00
    多谢大家,使用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输出的顺序呢?
    第 2 条附言  ·  2015-06-21 17:32:35 +08:00
    (http://www.cplusplus.com/reference/mutex/mutex/lock/)的例子跟我这个差不多,锁住之后就是正常按照顺序输出,晕.
    现在学长要求我讲SVM(支持向量机)一共1500多行放到多thread去执行。里面的算法是搞懂了,但是多个thread执行。感觉是没有什么思路。
    18 条回复    2015-06-22 10:08:19 +08:00
    aheadlead
        1
    aheadlead  
       2015-06-20 22:16:56 +08:00
    xdeng
        2
    xdeng  
       2015-06-20 23:10:25 +08:00
    0o0 按理说是一次性输出的
    limhiaoing
        3
    limhiaoing  
       2015-06-20 23:48:15 +08:00
    你看的那篇文章里写了这么一句话
    Actually if you run the above code on your system you can get a completely different result or even some mangled characters.
    limhiaoing
        4
    limhiaoing  
       2015-06-20 23:50:55 +08:00
    我也写过一篇关于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
        5
    zonyitoo  
       2015-06-21 00:03:15 +08:00
    乱掉很正常,你给的例子里它也是乱掉的,只是没有你的这么乱罢了。
    多线程同时调用cout,没有人能知道它到底是以什么样的顺序进入到cout的buffer里面。
    正确的姿势是用Mutex锁住cout。
    n0o0a0h0
        6
    n0o0a0h0  
    OP
       2015-06-21 00:06:25 +08:00
    @limhiaoing 谢谢 你的blog 写得好详细 我细细品味一下
    @aheadlead
    @zonyitoo
    谢谢两位 我是初学者啦~~~
    @xdeng 可以参考楼上的回答
    xdeng
        7
    xdeng  
       2015-06-21 00:30:38 +08:00
    原来是c++11 自带的线程
    aheadlead
        8
    aheadlead  
       2015-06-21 01:01:46 +08:00
    @n0o0a0h0 加油!
    secondwtq
        9
    secondwtq  
       2015-06-21 01:11:24 +08:00
    我在 Linux 下测试,结果和原文差不多。
    在 Mac 下测试,结果和楼主给的差不多。
    SoloCompany
        10
    SoloCompany  
       2015-06-21 01:55:45 +08:00
    头像好萌
    chlx
        11
    chlx  
       2015-06-21 02:07:15 +08:00
    即使满足sequential order也是乱掉的。 同楼上, 锁住cout
    middleware
        12
    middleware  
       2015-06-21 08:22:41 +08:00
    iostream 自己有同步。由 sync_with_stdio() 控制。
    shimoyo
        13
    shimoyo  
       2015-06-21 10:01:22 +08:00 via iPhone
    前段时间的操作系统课讲的进程的互斥,老师讲的例子几乎和这个一样……
    inevermore
        14
    inevermore  
       2015-06-21 13:33:35 +08:00
    你们啊,naive,最简单的方式是使用printf,他是默认原子性的。
    n0o0a0h0
        15
    n0o0a0h0  
    OP
       2015-06-21 17:16:21 +08:00
    @chlx 锁定了哦 不知道是否可以控制thread输出的是顺序呢?
    n0o0a0h0
        16
    n0o0a0h0  
    OP
       2015-06-21 17:16:40 +08:00
    @inevermore 其实也是初学这方面 ~~多谢批评
    Axurez
        17
    Axurez  
       2015-06-22 00:29:00 +08:00
    cppreference 的例子说了,Possible output (order of lines may vary, but they are never intermingled)
    并不是顺序的
    n0o0a0h0
        18
    n0o0a0h0  
    OP
       2015-06-22 10:08:19 +08:00
    @Axurez 好的~~~ 谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1299 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 17:40 · PVG 01:40 · LAX 10:40 · JFK 13:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.