Mac OS X:有可能使非主线程成为“主线程”的过程? [英] Mac OS X: is it possible to imbue a non-main thread to become "The Main Thread" of a process?

查看:288
本文介绍了Mac OS X:有可能使非主线程成为“主线程”的过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac OS X(10.6.7)下有一个GUI /线程相关的问题。
我使用wxWidgets框架(版本2.9.1),它在我的情况下基于Cocoa。应用程序设计如下:

I'm having a GUI/threading-related problem under Mac OS X (10.6.7). I'm using the wxWidgets framework (ver. 2.9.1), and it rests upon Cocoa in my case. The application design is like this:


  • 线程#1(也称为主线程):输入main(),解析开关,

  • 线程#2(也称为GUI线程):使用wxEntry初始化wxWidgets并显示GUI。

和大多数其他GUI框架一样,Cocoa不是线程安全的,所以我们确保从线程#2中执行所有GUI调用,传递消息if需要。
然而,在这种特殊情况下,在初始化期间(从NSUndoManager到更精确)在Cocoa的内部提出一个断言,在本质上说在主线程之外使用我是不安全的。即使线程#2是主线程,只要是与GUI相关的。

Like most other GUI frameworks, Cocoa is not thread-safe, so we make sure to do all GUI calls from within thread #2, passing messages if required. Yet, in that particular case, an assertion is raised from within Cocoa's internals during initialization (from NSUndoManager to be more precise) saying in essence "it's not safe to use me outside the main thread". Even though thread #2 is the main thread as far as anything GUI-related is concerned.

好吧,NSUndoManager必须有一个方法发现它运行的主线程(可能使用NSThread :: isMainThread())。所以我的问题是:是否可能欺骗NSUndoManager(和Cocoa一般)这个?更好的是,将线程#2声明为主线程,线程#1成为辅助线程?基本上,我需要一个API调用,使调用线程成为主要。没有文档的巫术和Objective C ++是好的,只要它也适用于OS X 10.5。

Well, NSUndoManager has to have a way to find out it's running off the main thread (probably using NSThread::isMainThread()). So my question is: is it possible to trick NSUndoManager (and Cocoa in general) about this? And even better, to declare thread #2 being "The Main Thread", with thread #1 becoming a secondary one? Basically, I need an API call like "make the calling thread become the Main One". Undocumented wizardry and Objective C++ is fine, as long as it works on OS X 10.5 as well.

P.P。代码,因为它现在,在Windows / Linux / MacOSX + Carbon下完美无缺地工作。此外,重新设计它以改变线程结构将是一个巨大的痛苦。

P.P. the code, as it is now, works flawlessly under Windows/Linux/MacOSX+Carbon. Also, redesigning it to change the thread structure would be a huge pain.

推荐答案

不会逃避代码的重构。大多数GUI系统为自己使用主线程和句柄事件循环。但是如果你说GUI是可选的,也许最好将应用程序分成两个工作和GUI。 GUI可以通过多种方式与工作者进行通信,具体取决于平台/特定需求。

OK, so according to your comment: you basically won't escape refactoring of your code. Most GUI systems use main thread and handle event loops for themselves. But if you say that GUI is optional, maybe it'd better to split your application into two -- worker and GUI. GUI could communicate with worker via numerous ways, depending on platforms/specific needs.

这篇关于Mac OS X:有可能使非主线程成为“主线程”的过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆