I am sorry for giving so much trouble! Thank you
very much for giving me much help.I know much about
it.
Don't worry! It is no trouble at all. The Apple Discussion forums are virtually my only entertainment during the day. It is a long story...
In window, there is
MsgWaitForMultipleObjectsEx.Its function is as
follow:
The MsgWaitForMultipleObjectsEx function returns
when one of the following occurs:
Either any one or all of the specified objects are
in the signaled state. The array of objects can
include input event objects, which you specify using
the dwWakeMask parameter.
An I/O completion routine or asynchronous procedure
call (APC) is queued to the thread.
The time-out interval elapses.
But I can not find the same function in cocoa.would
you help me please?
The multithreading primitives in the Win32 API are very powerful and nice to work with. They are more powerful than what is available in MacOS X. I would expect, but don't know for sure, that there are low-level, kernel functions in MacOS X that are as powerful as in the Win32 API. These are probably found somewhere in the Mach API or IOKit. This is typical of Unix-type operating systems. They will give you a basic POSIX API that is good for 95% of what you need. The other 5% is available only as a cryptic, low-level library that is different for each machine. Win32 is nice because these low-level primitives are very easy to use and much more accessible than in any other system.
But, that isn't your question. You want to know how to implement MsgWaitForMultipleObjectsEx in MacOS X. The function that comes to mind first is the POSIX select() function. I think whoever wrote MsgWaitForMultipleObjectsEx had select() in mind when they wrote it. At a high level, select() works very similarly to MsgWaitForMultipleObjectsEx - except that you wait on file descriptors instead of Events. Unfortunately, Events are much more powerful than file descriptors. If you are waiting for activity on a number of open sockets or files, you can just use select(). That is what it is for. I can honestly say that I prefer using MsgWaitForMultipleObjectsEx instead of select(). It is a cleaner way to do asynchronous I/O. I'm still trying to learn how to do that the way I want on MacOS X.
select() will not work for thread synchronization and communication. For that, you will have to use pthread conditions. They are kind of clunky and non-intuitive. You have to get a lock, then wait for a condition to happen, then release the lock. They key part is that, while your code is inside the pthread condition wait function, it unlocks the underlying mutex for you. I don't like the way that works. When I get time, I'm going to look for better asynchronous I/O and threading functions in MacOS X. I'm not happy with select() and pthreads. I definitely prefer MacOS X for just about everything. But for programming low-level asychronous I/O and multithreading, I miss Win32.
And if anyone out there is reading this, and really knows low-level MacOS X programming, and is disgusted by all the misinformation I've just published, please, please correct me. I would like to learn a better way to do it myself. I haven't had the time to look so far.