创建共享动态内存的最佳方法是什么?

编程入门 行业动态 更新时间:2024-10-25 02:20:28
本文介绍了创建共享动态内存的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个Win32 C ++程序,我有一些需要读/写/分配/释放共享数据数组的线程。它也应该可以从主线程访问,并在程序退出时释放。该程序没有任何类,只有一个主消息循环和一个在UI交互上启动线程的WndProc回调。

I'm writing a Win32 C++ program where I have a few threads that need to read/write/allocate/free a shared data array. It should also be accessible from the main thread and freed when the program exits. The program doesn't have any classes, just a main message loop and a WndProc callback that starts threads on UI interaction.

我创建了一个全局指针,我尝试使用运营商new / delete []来管理数组,并且malloc / free也取得了一些成功,但我不确定这是最好的方法。

I created a global pointer and I tried to use operators new/delete[] to manage the array, and also malloc/free with some success but I'm not sure it's the best way to do it.

组织的常用方法是什么并在多线程应用程序中管理这样的数据存储?

What's the common way to organize and manage such data storage in a multi-thread application?

谢谢!

推荐答案

几乎每次有人说"最好"的时候在一个问题中,他们几乎没有提供他们需要的标准。对于一种情况,最佳解决方案可能是另一种情况的最坏情况更重要的是,我可能会建议我认为最好的,而另外一些人可能会建议他们认为最好的其他东西。作品"最佳"通常被解释为主观的(意见问题),而不是客观的。

Nearly every time that someone says "best" in a question they provide nearly no criteria for what they need. The best solution for one situation might be the worst for another. More importantly, I might suggest what I think is best and someone else might suggest something else that they think is best. The work "best" is usually interpreted as something that is subjective (a matter of opinion), not objective.

我认为你需要提供更多信息。您读过哪些相关文章?你用的样品是什么?许多文章和书籍都有很多答案。

I think you need to provide more information. What relevant articles have you read? What samples are you using? There are many answers in many articles and books.

如果您只发布相关代码,那么这可能有所帮助。您有什么代码以及它有什么问题?

If you can post just the relevant code then that could help. What code do you have and what is the problem with it?

通常,答案是您分配要在线程之间共享的对象与在单个线程中使用的对象相同。唯一的区别是你需要同步访问;也就是说,你需要确保一次只有一个线程使用该对象,至少如果其中一个可能改变某些东西。

Generally, the answer is that you allocate objects to be shared among threads the same as objects for use within a single thread. The only difference is that you need to synchronize access; that is, you need to ensure that only one thread at a time uses the object, at least if one of them might change something.

以下是相关的部分。文档:

The following are relevant portions of the documentation:

  • 进程和线程
    • 关于进程和线程
      • 多个线程
        • 多线程的执行同步
        • Processes and Threads
          • About Processes and Threads
            • Multiple Threads
              • Synchronizing Execution of Multiple Threads

更多推荐

创建共享动态内存的最佳方法是什么?

本文发布于:2023-07-17 06:47:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1130535.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存   方法   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!