dispatch库使用
发表于|更新于
|字数总计:199|阅读时长:1分钟
libdispatch
libdispatch主要用于Apple平台, 称为GCD。也可在其他平台使用。
https://github.com/apple-oss-distributions/libdispatch
比如,swift语言目前跨平台,也使用libdispatch。
https://github.com/apple/swift-corelibs-libdispatch
一个简单例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #include <stdio.h> #include <stdlib.h> #include <dispatch/dispatch.h> #ifdef _WIN32 #include <Windows.h> #endif
int main() { dispatch_queue_t dq = dispatch_queue_create("com.test", DISPATCH_QUEUE_SERIAL); printf("%p\n", dq); dispatch_async(dq, ^{ #ifdef _WIN32 Sleep(1000); #else usleep(1000 * 1000); #endif puts("hello"); exit(0); }); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(500 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{ puts("world"); }); dispatch_main(); return 0; }
|
macOS
Linux
Windows
1 2 3 4
|
set PATH=%VS2022INSTALLDIR%\VC\Tools\Llvm\x64\bin;%cd%\x64\Debug\bin;%PATH% clang -fblocks -Ix64\Debug\include -Lx64\Debug\lib -lobjc -ldispatch dispatch.c
|
qt
dispatch_sync -> Qt::BlockingQueuedConnection
https://www.jianshu.com/p/19871d3209bf