我可以在主线程中使用 alloc() 而在另一个线程中使用 free() 吗?
我有一个程序可以在 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中的免费系统不是多线程的。
问题:
我应该重写我的程序,以便将要释放的内存块传递回主线程以在那里释放吗?
我应该尝试升级到更新的 mingw 吗?
除了大量测试之外,还有什么方法可以发现这些并发问题吗?这对我来说感觉不太好。
谢谢!
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:
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?
Should I try to upgrade to a more recent mingw?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么说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