以线程安全的方式每秒打印队列的当前元素数

Print current number of elements of queue in each second in thread safe manner

本文关键字:元素 队列 线程 安全 方式每 打印      更新时间:2023-10-16

我想在多线程程序中每秒打印数据队列的当前元素数,如下所示:

queue<int> products;
void print(ostream& s)
{
    cout << s.rdbuf();
    cout.flush();
    s.clear();
}
void printQueue()
{
    while(true)
    {
        this_thread::sleep_for(chrono::seconds(1));
        print(stringstream() << "Number of prouducts: " << products.size() << "n");
    }
}
void producer(int i)
{ //adds data into queue in thread safe manner
}
void consumer(int i)
{/*removes data from queue in thread safe manner*/}
int main()
{
  vector<thread> thrds;
    for(int i = 0; i < 5; ++i)
        thrds.push_back(thread(producer, i));
    for(int i = 0; i < 4; ++i)
        thrds.push_back(thread(consumer, i));
     thrds.push_back(thread(printQueue));
    for(auto& t : thrds)
        t.join();
}
return 0;
}

printQueue - 这个函数线程安全吗?

多个线程访问两个资源:

  • std::cout
  • products

std::cout的典型实现相对抗拒被多个线程访问,尽管它们不能保证输出不是交错的。不过,形式上没有同步,因此您的写入不是线程安全的。

关于products,您的访问也不受任何互斥锁的保护,这也使这些访问非线程安全。在某些地方你只阅读大小并不重要。并不是说这种读取访问可能会破坏任何东西,但结果(即当您看到更改和看到的内容时)仍然未定义。

摘要:使用互斥锁保护共享资源。

std::queue合约上printQueue线程安全与否。对于每个容器线程特定的方法行为,它们公开的方法将属于以下主要类别之一:

  • 线程同步
  • 线程安全
  • 线程不安全(可以生成争用)

第三个意味着此调用永远不能在不受保护的环境中使用 - 这意味着,在此调用发生时,不能对容器的任何其他方法进行其他调用。1 和 2 之间的区别在于,对于第一个选项,您可以保证在另一个线程中修改新值后读取新值(具有合理的延迟)。Thread safe 不会为您提供此保证,但仍声明调用此方法不会将程序暴露给数据争用,并且保证读取旧值或新值,但没有未定义的行为。

对于标准容器作为std::queue,它们的所有 const 方法是线程安全的,而非 const 方法是线程不安全的。由于您将调用线程不安全推送,因此您需要通过外部同步保护您的访问。