Objective C 中的线程

发布于 2024-11-09 15:50:22 字数 429 浏览 3 评论 0原文

我正在将 cpp 代码移植到 Objective C。 我是这个编程的新手。

我必须启动一个线程,该线程正在调用一个名为 timeoutThread 的函数。在一个名为 insert() 的函数中,它们使用 setEvent(m_Thread) 设置一个事件,在函数 timeoutThread 中,它们通过调用 waitforsingleobject(m_thread,在 waitforsingleObject 的上方,他们通过 setEvent(m_ThreadEvent) 设置另一个事件,并在超时线程函数内执行其他操作。 我创建了一个 NSOperationQueue 并使用 initWithTarget:toTarget:object 调用该函数。但是我如何设置事件和通知,就像它们在 Objective C 中的 cpp 中所做的那样。

任何关于此的简单示例的解释都会非常有帮助像我这样的初学者。

I am porting cpp code to Objective C.
I am new to this programming.

I have to start a thread this thread is calling a function called timeoutThread.and in one function called insert() they are setting an event with setEvent(m_Thread) and in the function timeoutThread they are waiting for the event by calling waitforsingleobject(m_thread,delay).and in just above the waitforsingleObject they are setting another event by setEvent(m_ThreadEvent).and doing the other stuffs inside the timeout thread function.
I have created a NSOperationQueue and called the function with initWithTarget:toTarget:object.But how do i set the events and notify like the same they are doing in cpp in Objective C.

Any explanation regarding this with a simple example would be very helpful for a beginner like me.

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

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

发布评论

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

评论(1

心安伴我暖 2024-11-16 15:50:22

我承认并不完全支持 Windows 做这些事情的方式,但我想如果你只等待单个标志,你想要的原语是 NSConditionLock。每个条件锁都有一个特定的条件,线程可以尝试锁定它而不考虑条件,或者仅当它具有特定的条件时,可选地为两者设置超时。然后,他们可以解锁,并且可以选择在解锁时设置新条件。

可能更直接的方法是手动创建 NSThreads,而不是仅仅将操作卸载到 NSOperationQueue 中。每个 NSThread 自动有一个 NSRunloop,因此您可以使用如下语义:

[object performSelector:@selector(operation:) onThread:targetThread withObject:someArgumentForOperation waitUntilDone:NO];

在这种情况下,一旦有机会出现,将使用指定线程上的指定参数调用方法“操作:”,并且调用线程不会被阻塞。 Runloop 解决了与经典 win32 消息调度机制相同的问题,但反转了责任 — Cocoa 处理阻塞线程、唤醒消息并发出适当的函数调用。

I admit not to being entirely up on the Windows way of doing these things, but I imagine the primitive you want if you're waiting on single flags only is NSConditionLock. Each condition lock has a particular condition, threads can attempt to lock it with no regard for the condition or only when it has a particular condition, optionally with a timeout for both. They can then unlock and, optionally, set a new condition when they do so.

Possibly a more straightforward approach is to create your NSThreads manually rather than just offloading operations into an NSOperationQueue. Each NSThread automatically has an NSRunloop so you can then use semantics like:

[object performSelector:@selector(operation:) onThread:targetThread withObject:someArgumentForOperation waitUntilDone:NO];

In which case the method 'operation:' will be called with the nominated argument on the nominated thread as soon as an opportunity arises, and the calling thread isn't blocked. Runloops solve the same problem as the classic win32 message dispatch mechanisms but invert responsibility — Cocoa deals with blocking threads, waking upon messages and issuing the appropriate function calls.

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