Shark Deng

Interactive Designer @Shanju Design in Shanghai

touched caffe / mxnet / python / wordpress / bootstrap / Arduino / Raspberry Pi / webgl / three.js / PIXI / 3dMax

Email

Github

RunLoop – 每隔4秒打印

2019-11-19 03:16:09

- (void)runloopThread {
    printf("test \n");
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(runloopExample) object:nil];
    [thread start];
}




-(void) runloopExample {
    //1
    CFRunLoopRef r = CFRunLoopGetMain();
    
    //2: src
    CFRunLoopSourceRef src;
    CFRunLoopSourceContext srcCtx;

    bzero(&srcCtx, sizeof(srcCtx));
    srcCtx.perform = _perform;
    
    src = CFRunLoopSourceCreate(NULL, 0, &srcCtx);
    CFRunLoopAddSource(r, src, kCFRunLoopCommonModes);
    
    //3: timer
    CFRunLoopTimerRef timer;
    CFRunLoopTimerContext timerCtx;

    bzero(&timerCtx, sizeof(timerCtx));
    timerCtx.info = src;
    
    timer = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent(), 4, 0, 0, _timer , &timerCtx);
    CFRunLoopAddTimer(r, timer, kCFRunLoopCommonModes);
    
    //4: run
    CFRunLoopRun();
    
    
}

static void _perform(void *info __unused) {
    printf("good \n");
    
}

static void _timer(CFRunLoopTimerRef timer __unused, void *info) {
    CFRunLoopSourceSignal(info);
}

 

运行 socket.runloopThread() (swift 运行oc,一个bridging header即可)



——By SharkDeng
如果你喜欢我的文章,欢迎红包赞赏

Leave a Reply

Your email address will not be published. Required fields are marked *