【问题标题】:How do I print in a new thread without threads interrupting lines? (particularly c++)如何在没有线程中断行的情况下在新线程中打印? (尤其是 C++)
【发布时间】:2014-12-11 03:57:19
【问题描述】:

我在 Linux 上的 C 中使用线程进行了相当多的工作,现在我尝试在 Windows 上使用 C++ 做同样的事情,但我在打印到标准输出时遇到了麻烦。在线程执行的函数中,我有:

void print_number(void* x){
    int num = *(static_cast<int*> (x));
    std::cout << "The number is " << num << std::endl;
}

包裹在一个创建三个线程的循环中。问题是,尽管所有内容都被打印出来,但线程似乎在每个“

例如,我上次运行它时得到了

The number is The number is 2The number is 3
1

当我希望每个都在单独的行上时。我猜每个线程都能够在另一个线程在“

【问题讨论】:

标签: c++ multithreading


【解决方案1】:

在 C++ 中,我们首先更喜欢将参数作为int*。然后,我们可以锁定。在 C++11 中:

std::mutex mtx; // somewhere, in case you have other print functions 
                // that you want to control

void print_number(int* num) {
    std::unique_lock<std::mutex> lk{mtx}; // RAII. Unlocks when lk goes out of scope
    std::cout << "The number is " << *num << std::endl;
}

如果不是 C++11,boost::mutexboost::mutex::scoped_lock 工作方式相同,做同样的事情。

【讨论】:

  • 请注意,在某些平台(例如 Windows)上,写入控制台非常昂贵(IIRC 每次调用至少 50 毫秒,因此此处给出的函数中的单个 cout 需要 150 毫秒)。在写入期间锁定通常意味着您基本上已经对“并行”功能进行了序列化。在这些情况下,您最好将控制台写入移至单独的线程并使用线程安全的消息传递方案将字符串传递给写入线程。
【解决方案2】:

您的 C 示例意外运行; printf 之类的 aren't atomic either.

这确实是互斥体的情况。我通常在本地分配它的静态函数。例如:

void atomic_print(/*args*/) {
    static MyMutex mutex;
    mutex.acquire();
    printf(/*with the args*/);
    mutex.release();
}

【讨论】:

  • @RaphaelM。确实;这是因为printf 根据标准是threadsafe,而不是(必然)atomic。请参阅我的答案中的第二个链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-13
  • 2016-09-22
  • 2021-04-13
  • 1970-01-01
相关资源
最近更新 更多