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

来源:互联网 发布:音乐动画相册制作软件 编辑:程序博客网 时间:2024/05/29 17:00

功能:

实现多线程: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文件头

staticEDUAppDelegate *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]







0 0