用 C++ 创建线程与两个单独的程序相比?
我正在制作一个应用程序,该应用程序在 C++ 中搜索请求的信息时接收用户输入。在 Windows 资源管理器中运行两个不同的应用程序会更好吗?会更快吗?
I'm making an application that receives user input while it is searching for the requested info in C++. Would it be better just to run two different applications in Windows Explorer? Would it be faster?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所描述的听起来像是多线程编程的典型用例。您将使主线程等待用户输入并启动其他线程来执行其他操作(例如搜索)。
另一种方法是设计两个必须相互通信和协调的独立进程,这会增加一些额外的开销。因此,只有在几乎不需要来回沟通的情况下,速度方面您才可能受益。
What you describe sounds like the typical use case for multi-threaded programming. You would have your main thread waiting for user input and start up additional threads to do other things like searching.
The other approach of designing two separate processes that will have to communicate and coordinate between each other will add some additional overhead. So speed-wise you might benefit only if there's little back-and-forth communication required.
答案取决于几个因素,例如整个过程需要多长时间,以及两个进程应在它们之间共享多少信息。
如果这只是几秒钟的事情,并且应用程序的两个部分之间共享一个数据结构,那么我认为将它们分成不同的进程没有任何意义。在线程之间共享内存要容易得多(只是不要忘记使用线程安全的数据结构)。
The answer depends on several factors, such as how much time does all this process take, and how much information should the two processes share between them.
If this is a matter of seconds, and there is a data structure shared between the two parts of your application, I don't see any point in separating them into distinct processes. It's much easier to share memory between threads (just don't forget to use thread-safe data structures).