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

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

本文介绍了创建共享动态内存的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个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?

谢谢!

推荐答案

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

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


              这篇关于创建共享动态内存的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:31