【发布时间】:2021-05-26 00:36:57
【问题描述】:
我正在用 C++20 编写。这是一个非常简单的程序,它要求三个线程打印一些字符串。例如,我们要求线程 1 打印 "This is thread1",线程 2 打印 "This is thread2",线程 3 打印 "This is thread3"。
但是,我注意到在传递给线程的printThread 函数中,如果我们不使用锁,我们可以获得混合在线程之间的打印结果。如This is This thread2 is thread3。我想避免这样的干预,所以我用mutex写了我的代码:
#include <iostream>
#include <string.h>
#include <thread>
#include <mutex>
using namespace std;
mutex m_screen;
void printCnt()
{
lock_guard guard(m_screen);
cout << "Func Start" << endl;
// Fetch the thread ID of the thread which is executing this function
thread::id threadID = this_thread::get_id();
cout << "Thread [" << threadID << "] has been called " endl;
}
// g++ foo.cpp =pthread
int main(){
thread t1(printCnt);
thread t2(printCnt);
thread t3(printCnt);
t1.join();
t2.join();
t3.join();
cout << "done" << endl;
}
不知道有没有什么方法可以达到和互斥锁一样的效果,但是没有锁?
【问题讨论】:
-
std::osyncstream?当它刷新到 std::cout 时,它可能会在内部使用锁定,但这仍然比在流式传输期间锁定更好。
-
std::osyncstream仍将使用某种形式的锁。 -
您可以构造要输出的整个字符串,然后将其传递给
cout。这将有一个函数调用(而不是四个)。 -
您也许可以通过某种方式使用原子库。
标签: c++ multithreading c++11 c++20