我可以在主线程中使用 alloc() 而在另一个线程中使用 free() 吗?

发布于 2024-10-05 09:09:04 字数 622 浏览 0 评论 0原文

我有一个程序可以在 MacOS 和 Linux 上运行良好,并使用 mingw 交叉编译到 Windows。最近我把程序变成了多线程。

该程序的当前设计在主线程中分配了内存,并在从属“工作”线程中释放了内存。这在 MacOS 和 Linux 上不是问题,因为 malloc/free 系统是多线程的。

然而,我担心交叉编译。我使用的 mingw 版本是从 MacOS 端口构建的。这是 2004 年的一个相当古老的 G++ 版本(版本 3.4.5)。我尝试构建更新的版本但没有成功(我想构建 64 位版本,但放弃了)。我从 http://sourceware.org/pthreads-win32 获取 pthread。

我担心的是 malloc 和3.4.5中的免费系统不是多线程的。

问题:

  1. 我应该重写我的程序,以便将要释放的内存块传递回主线程以在那里释放吗?

  2. 我应该尝试升级到更新的 mingw 吗?

  3. 除了大量测试之外,还有什么方法可以发现这些并发问题吗?这对我来说感觉不太好。

谢谢!

I have a program that runs fine on MacOS and Linux and cross-compiles to Windows with mingw. Recently I made the program multi-threaded.

The current design of the program has memory allocated in the main thread and freed in the slave "worker" threads. That's not a problem on MacOS and Linux because the malloc/free system is multi-threaded.

I'm concerned about the cross-compiling, however. The version of mingw that I'm using is built from MacOS ports. It's a pretty ancient version of G++ (version 3.4.5) from 2004. I've been unsuccessful in my attempts to build a more recent version (I'd like to build a 64-bit version, but gave up). I'm getting pthreads from http://sourceware.org/pthreads-win32.

My concern is that the malloc & free system in 3.4.5 is not multi-threaded.

Questions:

  1. Should I rewrite my program so that the blocks of memory to be freed are passed back to the main thread to be freed there?

  2. Should I try to upgrade to a more recent mingw?

  3. Is there any way to find these concurrency problems other than massive amounts of testing? That just doesn't feel good to me.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请止步禁区 2024-10-12 09:09:04

为什么说malloc & free不是多线程吗?

mingw32 默认情况下会与 msvcrt.dll 链接,这是一个多线程 dll。参见[1]。 Microsoft 提供了[2]一个单线程库,但它仅可用于静态链接。

PS:你提到你正在交叉编译,但你似乎是在Windows中编译Windows程序。在这种情况下,为什么不从 www.mingw.org 下载二进制文件呢? (不过,在下载中找出所需的文件是一件很痛苦的事情)

1- http://msdn.microsoft.com/en-us/library/abx4dbyh%28v=VS.71%29.aspx

2- 请参阅 [1]。在 Visual Studio 2005 中删除 http://msdn.microsoft.com/en-us/library/abx4dbyh%28v=VS.80%29.aspx

Why do you say malloc & free are not multithreaded?

mingw32 by default will link with msvcrt.dll which is a multithread dll. See [1]. There was[2] a single-threaded library provided by Microsoft, but it was only available for static linking.

PS: You mention that you are cross-compiling but you seem instead to be compiling the windows program in windows. In such case, Why don't you dowload the binaries from www.mingw.org? (it's a pain to figure out in their downloads the files needed, though)

1- http://msdn.microsoft.com/en-us/library/abx4dbyh%28v=VS.71%29.aspx

2- See [1]. Removed in Visual Studio 2005 http:// msdn.microsoft.com/en-us/library/abx4dbyh%28v=VS.80%29.aspx

听风念你 2024-10-12 09:09:04
  1. 我会避免这种情况。听起来你试图回避主要问题。
  2. 是的,在任何情况下这都是一个好主意......
  3. 检测与内存分配/释放相关的并发问题的一种方法是内存泄漏检测器。我不确定 valgrind 是否适用于 cygwin。
  1. I would avoid this. It sounds like you're trying to dodge the main issue.
  2. Yes, that would be a good idea in any case...
  3. One way to detect concurrency problems related to memory allocation/deallocation is a memory leak detector. I'm not sure if valgrind works on cygwin.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文