MAC COCOA一个简单的多线程程序

功能:

实现多线程:2个线程同一时候工作,一个用时间计数器。一个用来信息打印

STEP1

XCODE -》New Application -》Cocoa中的Command Line

自己主动添加:

#include <CoreFoundation/CoreFoundation.h>


STEP2

//
//  main.c
//  test_runloop1
//
//  Created by DMD on 20/6/14.
//  Copyright (c) 2014 EDU. All rights reserved.
//

/*
  Test Thread 
 */

#include <CoreFoundation/CoreFoundation.h>

// Just for this c file.
static int g_vid=1;

static void _perform(void *info __unused)
{
    printf("No %d. hello,\n",g_vid);
    g_vid++;
}

static void _timer(CFRunLoopTimerRef timer __unused, void *info)
{
    g_vid++;

    CFRunLoopSourceSignal(info);
}

int main(int argc, const char * argv[])
{
    // insert code here...
    CFRunLoopSourceRef source;
    CFRunLoopSourceContext source_context;
    
    
    // 第一个线程:运行自己定义的函数:_perform
    bzero(&source_context, sizeof(source_context));
    //调用要运行的函数
    source_context.perform = _perform;
    //声称循环源
    source = CFRunLoopSourceCreate(NULL, 0, &source_context);
    //将循环源添加到当前线程里面去
    CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
    
    // 第二个线程:一个时间计数器
    CFRunLoopTimerRef timer;
    CFRunLoopTimerContext timer_context;
    
    bzero(&timer_context, sizeof(timer_context));
    timer_context.info = source;
    //生成时间循环源
    timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent(), 1, 0, 0,
                                 _timer, &timer_context);
    //添加时间循环器
    CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);
    
    CFRunLoopRun();
    return 0;
}

以上代码可读性不高,能够看以下的代码

//

//  main.c

//  test_runloop1

//

//  Created by DMD on 20/6/14.

//  Copyright (c) 2014 EDU. All rights reserved.

//


/*

  Test Thread 

 */


#include <CoreFoundation/CoreFoundation.h>


// Just for this c file.

staticint g_vid=1;


staticvoid _perform(void *info__unused)

{

    printf("No %d. hello,\n",g_vid);

   g_vid++;

}


staticvoid _timer(CFRunLoopTimerRef timer__unused,void *info)

{

   g_vid++;


    CFRunLoopSourceSignal(info);

}


int main(int argc,constchar * argv[])

{

    // insert code here...

    CFRunLoopSourceRef source;

    CFRunLoopSourceContext source_context;

    

    

    // 第一个线程:运行自己定义的函数:_perform

   bzero(&source_context,sizeof(source_context));

    //调用要运行的函数

    source_context.perform =_perform;

    //声称循环源

    source =CFRunLoopSourceCreate(NULL,0, &source_context);

    //将循环源添加到当前线程里面去

    CFRunLoopAddSource(CFRunLoopGetCurrent(), source,kCFRunLoopCommonModes);

    

    //第二个线程:一个时间计数器

    CFRunLoopTimerRef timer;

    CFRunLoopTimerContext timer_context;

    

   bzero(&timer_context,sizeof(timer_context));

    timer_context.info = source;

    //生成时间循环源

    timer = CFRunLoopTimerCreate(NULL,CFAbsoluteTimeGetCurrent(),1,0,0,

                                _timer, &timer_context);

    //添加时间循环器

    CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer,kCFRunLoopCommonModes);

    

//假设不用,就会在运行中。能够做其它的事件。

  //  CFRunLoopRun();

   return0;

}




如图

測试成功。

參考:

http://blog.csdn.net/onlyou930/article/details/7423161


重要补充:

CFRunLoopRun();

以上函数,假设不用,就会在运行中。能够做其它的事件。假设用了这个函数。就会在xib界面中运行中将界面卡住。无法去做其它事情。


- (IBAction)OnBT_Stop:(id)sender

{

    m_window_main.public_integer = 0;

}

会出现一个编译成功,可是执行不了,提示:请选择能够执行的框架,原因是:m_window_main 是本身的类,在本身里面不能前面增加自己。


另外,假设在一个m文件中面调用非类函数或者变量。比如在m里面写的C++函数。

须要额外申明这个类的变量指针。而且初始化函数里面 =self

比如在m文件头

static EDUAppDelegate *m_window_main;

初始化函数里面须要

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

{

    // Insert code here to initialize your application

    m_window_main = self;

}

这样才干够在其它非类函数里面调用类H定义的各种变量和函数。

调用函数的方法:

m_window_main funct:var1];

也能够直接调用里面的变量Control

m_window_main.m_lable1=@"aaa";


[END]







posted @ 2017-04-19 20:06  gccbuaa  阅读(666)  评论(0编辑  收藏  举报