简体   繁体   English

C ++多线程指针指向的指针

[英]C++ multithreaded pointer to pointer to pointer

Let's say I have a pointer to pointer to pointer, ie int*** ptr, where it holds a three dimensional array. 假设我有一个指向该指针的指针,即int *** ptr,它保存一个三维数组。 Depending on the CPU, I want to have approximately three threads writing and reading at the same time my pointer. 根据CPU,我希望大约有三个线程同时读写我的指针。 For the purpose of simplicity, the ptr won't be redimensioned (I still want to know how to handle multithreading if I had to redimension the pointer). 为了简单起见,将不重新定义ptr(如果我不得不重新定义指针,我仍然想知道如何处理多线程)。 I want to write concurrently to the pointer without having to use mutexes because it would block other threads. 我想并发写入指针而不必使用互斥体,因为它会阻塞其他线程。 How would I do that? 我该怎么做? I still don't quite understand the atomic template clas... 我仍然不太了解原子模板分类...

Thank you! 谢谢! :) :)

It depends on how your threads are running. 这取决于线程的运行方式。

All threads can read from a given location without conflict, but it is changing a value (changing the value at the same time, reading a value while it is changing, or using a read value that has changed) that is the problem. 所有线程都可以从给定位置读取而不会发生冲突,但是这是问题所在(同时更改值(同时更改值,在更改时读取值或使用已更改的读取值))。

If you've guarenteed that no value will be changed between threads (for example, thread 1 writes to ptr[0][0][x], thread 2 ptr[0][1][x], thread 3 ptr[0][2][x]) you actually don't need to do any mutexes at all. 如果您已确保在线程之间不更改任何值(例如,线程1写入ptr [0] [0] [x],线程2 ptr [0] [1] [x],线程3 ptr [0) ] [2] [x]),实际上您根本不需要执行任何互斥。

If you do not have that guarantee, then you have to mutex every read and write of the values. 如果没有保证,则必须互斥每次读取和写入值。

Redimensioning is essentially changing the pointer value. 重新定义实际上是在更改指针值。 So if only 1 thread has to deal with the pointer value that is being changed, you don't have to worry about it. 因此,如果只有1个线程必须处理要更改的指针值,则不必担心。 Else, mutex on every read and write. 否则,每次读取和写入时都使用互斥锁。 So if only 1 thread ever accesses ptr[0][0], and you need to resize ptr[0][0], then you are golden. 因此,如果只有1个线程访问ptr [0] [0],并且您需要调整ptr [0] [0]的大小,那么您就很高兴了。

If you do not have that guarantee, then you have to mutex every read and write of the values. 如果没有保证,则必须互斥每次读取和写入值。

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

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