简体   繁体   English

C ++-将数据复制到2个不同线程的最快/可靠方法

[英]C++ - Fastest/reliable method of copying data to 2 different threads

Suppose I have 3 threads: A, B, and C. Thread B is a critical thread that needs to be as fast as possible while the other two threads are not as important. 假设我有3个线程:A,B和C。线程B是关键线程,需要尽可能快,而其他两个线程则不那么重要。 I am not using any mutexes for performance reasons as well. 由于性能原因,我也没有使用任何互斥锁。

I receive an update for an object in thread A. It is an object that is allocated on the heap. 我在线程A中收到一个对象的更新。它是在堆上分配的对象。 Thread A transfers this data to thread B via posting an event to an event queue for thread B. Thread B will update its copy of the object by clearing out the old memory and resetting the pointer to the new memory. 线程A通过将事件发布到线程B的事件队列中来将该数据传输到线程B。线程B将通过清除旧内存并将指针重置为新内存来更新其对象副本。 Now there is a 3rd thread involved (thread C) that also needs this data whenever thread A receives the update. 现在,涉及到第三个线程(线程C),只要线程A收到更新,该线程也需要此数据。

If I try to transfer this data from thread B to thread C, then theoretically, thread B could be clearing out old memory at the same time that thread C is working with that chunk of memory (which will cause a race condition). 如果我尝试将数据从线程B传输到线程C,那么从理论上讲,线程B可能正在清理旧内存,而线程C正在处理该内存块(这将导致争用条件)。 It is OK if thread C is working with an old set of data. 如果线程C正在使用旧数据集,则可以。 I just want to prevent the race condition. 我只是想防止比赛情况。 So in that case, should I have thread A create 2 copies of the data (one for thread B and one for thread C) and post an event to each of them? 因此,在那种情况下,我是否应该让线程A创建2个数据副本(一个用于线程B,一个用于线程C)并将一个事件发布到每个副本? I don't want to create a copy while in thread B because that thread needs to be as fast as possible. 我不想在线程B中创建副本,因为该线程需要尽可能快。

I'm not 100% sure what the question is. 我不是100%知道问题是什么。 If it is about memory management (ie- when to free the memory), just use a shared_ptr (either the C++11 or boost). 如果是关于内存管理的(即何时释放内存),则只需使用shared_ptr(C ++ 11或boost)。 Then the last thread to get rid of the pointer will release the memory. 然后最后一个摆脱指针的线程将释放内存。

Also, you claim you don't have any mutexes, but you are using an event queue. 另外,您声称您没有任何互斥锁,但您使用的是事件队列。 That event queue has a mutex in it. 该事件队列中有一个互斥体。 If it works fast enough for you, in all likelihood, a mutex at the right place will too. 如果它足够快地为您工作,那么在正确位置的互斥体也很有可能。

I don't know whether I fully understand the question, correct me if I am wrong. 我不知道我是否完全理解这个问题,如果我错了,请纠正我。

So the problem here is that thread B and thread C may working on the same chunk of data and you don't want use lock considering the performance. 因此,这里的问题是线程B和线程C可能在同一数据块上工作,并且您不希望使用锁来考虑性能。

So why not add a DataPool layer between thread B and thread C? 那么,为什么不在线程B和线程C之间添加一个DataPool层呢? This DataPool will preallocate enough memory and your thread B will ask it for new memory. 该DataPool将预分配足够的内存,并且您的线程B会要求它提供新的内存。 Once it's done, thread B pass the pointer to a blocking queue and thread C will start working on that. 完成后,线程B将指针传递到阻塞队列,线程C将开始在该队列上工作。 Once thread C's done, it will call the DataPool to recycle. 一旦线程C完成,它将调用DataPool进行回收。

It is possible that thread B may wait for thread C because DataPool doesn't have any free memory and everyone is waiting for thread C to call DataPool to release memory. 线程B可能等待线程C,因为DataPool没有任何可用内存,并且每个人都在等待线程C调用DataPool释放内存。 Firstly, you could enlarge the dataPool and test what's the best size to avoid the problem. 首先,您可以扩大dataPool并测试最佳大小以避免问题。 Secondly, if thread C is not so important, DataPool could force thread C to terminate and get some free memory. 其次,如果线程C不是那么重要,则DataPool可以强制线程C终止并获得一些可用内存。

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

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