我正在學習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