我正在学习Mac App开发,从命令行应用程序和Core Foundation API开始。我想要做的是在应用程序在终端中运行时监听文件系统事件。当用户退出时,它会干净地关闭流并退出。以下是我有...OSX命令行C应用程序中的线程管理
#include <CoreServices/CoreServices.h>
#include <stdio.h>
void eventCallback(FSEventStreamRef stream, void *callbackInfo, size_t numEvents, void *paths, FSEventStreamEventFlags flags[], FSEventStreamEventId eventId[]) {
printf("Test");
}
int main(int argc, const char * argv[])
{
CFStringRef mypath = CFSTR("/Path/to/folder");
CFArrayRef paths = CFArrayCreate(NULL, (const void **)&mypath, 1, NULL);
CFRunLoopRef loop = CFRunLoopGetMain();
FSEventStreamRef stream = FSEventStreamCreate(NULL, eventCallback, NULL, paths, kFSEventStreamEventIdSinceNow, 3.0, kFSEventStreamCreateFlagNone);
FSEventStreamScheduleWithRunLoop(stream, loop, kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
bool done;
# Somehow put main thread to sleep here...
# On exit of application
FSEventStreamStop(stream);
FSEventStreamInvalidate(stream);
FSEventStreamRelease(stream);
return 0;
}
所以我决定,使用任一主线程运行循环(或者一个单独的线程)来做这件事情,但我不知道其中的最佳途径让线程在等待事件时进入睡眠状态。我对苹果的API不太了解,不知道该怎么做。
感谢您的帮助!
我鼓掌你的努力! – trojanfoe