线程间“通信”;问题

发布于 2024-11-02 12:29:11 字数 244 浏览 0 评论 0原文

在我的应用程序中,我有两个线程,第一个是 gui 线程,其中我有代码 当用户按下每个按钮时调用。

在第二个线程中,我有视频录制机制,我可以在其中启动、捕获视频并将视频附加(录制)到文件中。

当我尝试从第一个线程“开始录制”时出现问题 - 它崩溃了。 所以我必须在 gui 线程中设置某种布尔“通信标志”,然后检查它们 在视频线程中,然后从视频线程“录制视频”。

我认为这样的标志很糟糕 - 什么是简单且正确的方法来做到这一点?

In my app, I have two threads, first is the gui thread where I have code which
is called when user presses each button.

In second thread I have video recording mechanics, where I can start, capture, append (record)video to the file.

Problem arises when I am trying to 'start recording' from first thread - it crashes.
So I must set some kind of boolean 'communication flags' in gui thread and then check them
in video thread and then 'record video' from the video thread.

Such flags are bad I think - what is an easy and proper way to do that?

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

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

发布评论

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

评论(1

命比纸薄 2024-11-09 12:29:11

这是一个相当广泛的话题。不过,Cocoa 确实包含一些有用的线程间通信方法。您可能想看看

[NSObject performSelector:onThread:withObject:waitUntilDone:]

只有在第二个线程中运行 NSRunLoop 时才有效。否则,您的视频录制类中有一个 bool @property。这将是您建议的旗帜。从主线程更改其值,并从记录循环中的辅助线程检查其值以执行操作。

不要忘记您的 @property 声明必须是原子的。

您可能还想使用 NSOperationQueue 或块,很难从您的问题中看出什么最有效。我想这也取决于您的编码偏好。

That's quite a broad topic you are touching there. Cocoa does contain some useful interthread communication methods though. You might want to look at

[NSObject performSelector:onThread:withObject:waitUntilDone:]

That will only work if you have a NSRunLoop running in the second thread. Otherwise have a bool @property in your video recording class. This will be the flag, that you suggested. Change its value from the main thread and check its value from the secondary thread in your recording loop to perform an action.

Don't forget that your @property declaration needs to be atomic.

You might also want to use NSOperationQueue or blocks, it's hard to tell from your question what would work best. Depends on your coding preferences as well, I guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文