测试了PlayBook NDK上编写的控制台程序后,马上想进一步测试的就是PlayBook NDK上的多线程编程。经过测试很开心地发现PlayBook NDK上进行多线程编程和之前的QNX编程环境是相同的。也就是说在程序中可以简单地调用pthread_create方法实现线程的创建。
如此简单的事情就不再啰嗦了,直接上代码,供各位参考:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> void new_Thread() { fprintf(stderr, "..............in the Thread\n"); int pthread_t = pthread_self(); int i = 0; for (i = 0; i < 10; i++) { fprintf(stderr, "......%d........in the Thread %d \n", i, pthread_t); sleep(1); } } int main(int argc, char *argv[]) { fprintf(stderr, "starting my thread test...\n"); pthread_create(NULL, NULL, &new_Thread, NULL); int i = 0; for (i = 0; i < 20; i++) { fprintf(stderr, "in the main loop! %d \n", i); sleep(1); } return 0; }