简体   繁体   English

多线程一读一写time_t

[英]multithreaded one read one write time_t

I'm writing a multithreaded application that have 2 threads. 我正在编写一个具有2个线程的多线程应用程序。 One of the threads receives data from a queue and aggregates it and the other one send the aggregated data to a server. 其中一个线程从队列中接收数据并将其聚合,另一个线程将聚合后的数据发送到服务器。

I want to be able to know the last time that a data was received so I use: 我想知道上次收到数据的时间,所以我使用:

time_t last_data = time(NULL)

to get the correct time on each event (I dont need it to be super accurate but I need it to be fast) and then the other send this value with the aggregated data. 以获得每个事件的正确时间(我不需要它是非常准确的,但我需要它是快速的),然后另一个将这个值与聚合数据一起发送。

My questions are: 我的问题是:

  1. Do I have to synchronize this even if this is not very important that I get the most recent update? 即使获得最新更新对我来说不是很重要,我也必须同步吗?
  2. I tested it with std::atomic<time_t> and it seems to have some performance issues, is there any other faster way? 我用std::atomic<time_t>进行了测试,似乎有一些性能问题,还有其他更快的方法吗?
  3. What would be the worst case that can happen if I will not synchronize the read/write? 如果我不同步读/写,会发生什么最坏的情况?
  4. Is there a faster way to get the current time then time(NULL) (don't have to be super accurate)? 有没有一种更快的方法来获取当前时间,然后再获取time(NULL) (不必非常准确)?

UPDATE UPDATE

Here is an explanation of my application workflow. 这是我的应用程序工作流程的说明。

Application needs: 1. Consume data from external sources using IPC (currently nanomsg ). 应用程序需求:1.使用IPC(当前为nanomsg )从外部源消耗数据。 2. Aggregate the data to bulks. 2.汇总数据。 3. Send the aggregated data to remote server every given interval (1 second). 3.每隔给定间隔(1秒)将聚合数据发送到远程服务器。

Current implementation: 当前实施:

  1. Create 2 buffers to hold the aggregated data (one for receiving and one for sending). 创建2个缓冲区来保存聚合的数据(一个用于接收,一个用于发送)。
  2. Create a consumer thread to consume data from IPC and fill the receiving buffer. 创建使用者线程以使用IPC中的数据并填充接收缓冲区。
  3. Create a sending thread that will send the data to the server. 创建一个发送线程,它将数据发送到服务器。
  4. Every iteration of the interval the sending thread will swap the buffers (swap pointers and locking using mutex) and send the data to the server. 间隔的每次迭代,发送线程都会交换缓冲区(交换指针和使用互斥锁进行锁定),并将数据发送到服务器。

I don't want that the consumer will wait on network IO so I have created this flow. 我不希望使用者等待网络IO,所以我创建了此流程。 Can I use event driven here instead of this complex mechanism without all the locking (currently it is working fine but i'm sure it can be better)? 我可以在没有所有锁定的情况下在这里使用事件驱动来代替这种复杂的机制吗(当前它运行良好,但我敢肯定它会更好)?

Don't do it that way. 不要那样做。 You only need one thread. 您只需要一个线程。 You can use select/poll/epoll. 您可以使用选择/轮询/ epoll。 These can wait on your inputs, and at the same time for you output to finish. 这些可以等待您的输入,同时也可以完成输出。 You will be doing event driven programming, and non-blocking output. 您将进行事件驱动的编程和非阻塞输出。 It is something worth learning. 这是值得学习的东西。 It is a bit harder at first, but soon makes life easier ie not having the problem that you have now. 起初比较困难,但是很快会使生活更轻松,即不会遇到您现在遇到的问题。 Also the program will be faster. 而且程序会更快。

Supposing one thread executes: 假设执行一个线程:

last_data = time(NULL);

And the other uses last_data but there is no synchronization event between the two then there are no guarantees when or if the revised value of last_data will become visible to the reading thread. 而另一个使用last_data但两者之间没有同步事件,那么不能保证何时或是否将last_data的修改后的值对读取线程可见。

However the most serious possibility is that write of time_t (maybe long ) isn't atomic and another thread could read a corrupt 'part written' value. 但是,最严重的可能性是time_t写入(可能是long )不是原子的,并且另一个线程可能读取了损坏的“部分写入”值。 That could cause glitches in delay and time calculations that might foul downstream process. 这可能会导致延迟和时间计算故障,从而可能干扰下游流程。

You might analyse your program and find that because the two interact there is a sufficient memory fence at some point that guarantees eventual update. 您可能会分析您的程序,并发现由于两者相互影响,因此在某个时刻有足够的内存屏障可以保证最终更新。

NB: This is an odd situation where I'm suspecting you think something isn't synchronized but it is! 注意:这是一种奇怪的情况,我怀疑您认为某些内容未同步,但实际上是同步的! The usual experience is the other way around... 通常的体验是相反的...

Basically there's not really enough information to understand what problem you're having. 基本上没有足够的信息来了解您遇到的问题。 For example, if the reader thread is the only process to read the time I'd expect to see code like: 例如,如果读者线程是唯一读取时间的进程,那么我希望看到如下代码:

Thread 1: If data received, lock L, update time, add to queue, unlock L. Thread 2: If items in queue L, read queue and update time, unlock L .. process item. 线程1:如果接收到数据,则锁定L,更新时间,添加到队列,解锁L。线程2:如果队列L中的项目,读取队列和更新时间,解锁L ..处理项目。

In which case the time will be synchronized already. 在这种情况下,时间将已经同步。

Please provide a minimum, complete, verifiable example... 请提供一个最小,完整,可验证的示例...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM