windows 哪个更适合窗户?pthreads 还是 CreateMutex?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1110401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which is better for windows? pthreads or CreateMutex?
提问by puffadder
I am porting my application to windows from Linux. I am fairly new to the fine-art of porting application across platforms. As far as I know, Windows does not natively support POSIX threads implementation. Is this true? I have heard about some implementation of pthreads for windows (a wrapper or something), would it be better to use them or use CreateMutex and other APIs provided by windows???? Someone pls. enlighten me with the PROs and CONs of both worlds. Some miscellaneous tips for porting would go nicely along with the answer.
我正在将我的应用程序从 Linux 移植到 Windows。我对跨平台移植应用程序的艺术相当陌生。据我所知,Windows 本身并不支持 POSIX 线程实现。这是真的?我听说过 Windows 的 pthreads 的一些实现(包装器或其他东西),使用它们或使用 Windows 提供的 CreateMutex 和其他 API 会更好吗????有人请。用两个世界的优点和缺点启发我。一些有关移植的杂项提示将与答案很好地结合在一起。
Thanks in advance.
提前致谢。
采纳答案by Craig
One thing you need to keep in mind is what is the future of this code. Do you plan on developing (and releasing) on both platforms in the future? Or is this a one way port?
您需要记住的一件事是这段代码的未来是什么。您计划将来在两个平台上开发(和发布)吗?或者这是一个单向端口?
The best thing to do when porting a project is to keep the actual changes to the code as minimal as possible. In your case, this would mean going with a pthread solution. That being said, if you are planning this to be a one way port, going native never hurts. :)
移植项目时最好的做法是尽可能减少对代码的实际更改。在您的情况下,这意味着使用 pthread 解决方案。话虽如此,如果您计划将其作为单向港口,那么本地化永远不会受到伤害。:)
I would take some time to fully examine both stratigies and then implement the one you feel most comfortable with.
我会花一些时间来全面检查这两种策略,然后实施您觉得最舒服的一种。
回答by Ana Betts
It's all going to be the same stuff (pthreads is just going to call EnterCriticalSection etc), so if you've got a pthreads wrapper, you should probably use it so that you don't have to change as much code
一切都是一样的(pthreads 只是会调用 EnterCriticalSection 等),所以如果你有一个 pthreads 包装器,你可能应该使用它,这样你就不必更改那么多代码
回答by Ana Betts
this works well: http://sourceware.org/pthreads-win32/
这很有效:http: //sourceware.org/pthreads-win32/
It is a port of the pthreads library for Windows.
它是 Windows 的 pthreads 库的一个端口。
回答by Shing Yip
The first thing I'd do is to port to Boost Thread under Linux than to Windows.
我要做的第一件事是移植到 Linux 下的 Boost Thread 而不是 Windows。
回答by Duck
Why not have the best of both worlds and use a library that wraps both pthreads and Window's API and uses the appropriate one under the covers? Your code stays the same on both platforms.
为什么不两全其美,并使用一个包含 pthreads 和 Window 的 API 的库,并在幕后使用适当的库?您的代码在两个平台上保持不变。
There are no shortage of such libs in C++ so I can't imagine there aren't C versions about.
C++ 中不乏这样的库,所以我无法想象没有 C 版本。
回答by Remus Rusanu
On Windows C/C++ applications that use the CRT need to call beginthread/beginthreadexto properly initialize the CRT in the new thread.
在使用 CRT 的 Windows C/C++ 应用程序上,需要调用beginthread/beginthreadex以在新线程中正确初始化 CRT。