Lab 1
Can pthread be used on iOS as on linux?
The following code is available:
//a.h#ifndef __A_H__#define __A_H__void testSleep(int t);void testPthread(int n);#endif//a.c#include "a.h"#include <stdio.h>#include <pthread.h>#include <unistd.h>#include <stdlib.h>void testSleep(int t){ printf("testSleep:\n"); int i; for (i=0; i<10; i++) { printf("sleep...\n"); usleep(t*1000); printf("return...\n"); }}void *thrFun(void *p){ int t = (int)p; int i; for (i = 0; i<5; i++) { printf("thrFun %d\n", t); sleep(1); }}void testPthread(int n){ void *retval; pthread_t *tid = (pthread_t *)malloc(sizeof(pthread_t)*n); int i; for (i=0; i<n; i++) pthread_create(&tid[i], NULL, thrFun, (void *)i); for (i=0; i<n; i++) pthread_join(tid[i], &retval);}
Compile to a static Library:
Gcc-c-arch i386 a. c
Ar-r liba. a. o
Create a new project and add a. h and liba. a to the project.
Call testPthread (5) in a function)
An error occurred while running in the simulator:
Detected an attempt to call a symbol in system libraries that is not present on the iPhone:
Pthread_join $ UNIX2003 called from function testPthread in image test-lib1.
ThrFun 1
Detected an attempt to call a symbol in system libraries that is not present on the iPhone:
Sleep $ UNIX2003 called from function thrFun in image test-lib1.
ThrFun 2
ThrFun 3
ThrFun 4
ThrFun 0
Obviously, it cannot be used exactly as in linux.
------------------------ Split line ---------------------------------
I thought pthread could not be used on iOS.
Nm liba.:
Liba. a (a. o ):
U _ malloc
U _ printf
U _ pthread_create
U _ pthread_join $ UNIX2003
U _ puts
00000000 T _ testPrint
00000100 T _ testPthread
00000020 T _ testSleep
000000a0 T _ thrFun
U _ usleep $ UNIX2003
It seems that it is a problem of compilation options. The simple use of-arch i386 is not enough, it should be that a certain option is missing.
----------------------- Split line -------------------------------------
Use xcode to compile
Create a project. For example, select Cocoa Touch Static Library and name it abc.
After the file is created, delete the two files automatically generated above, and drag the two files a. h a. c above into the project.
Then select build
Libabc. a is always in red (that is, the file does not exist)
In fact, the red color disappears only when you have compiled the iOS Device version.
The libabc. a of the simulator version has been successfully generated. Where is it?
You can refer to this path to find
/Users/robin/Library/Developer/Xcode/DerivedData/abc-atlqvsfwteridoabdmjccgcalgnu/Build/Products/Debug-iphonesimulator
/Users/robin/Library/Developer/Xcode/DerivedData/abc-atlqvsfwteridoabdmjccgcalgnu/Build/Products/Debug-iphoneos
The former is the output directory of the simulator version.
The latter is the output directory of the simulator version.
Nm libabc. a output:
Libabc. a (a. o ):
000007c8 s EH_frame0
U _ malloc
U _ printf
U _ pthread_create
U _ pthread_join
00000000 T _ testPrint
000007e0 S _ testPrint. eh
00000130 T _ testPthread
00000834 S _ testPthread. eh
00000030 T _ testSleep
000007fc S _ testSleep. eh
000000c0 T _ thrFun
00000818 S _ thrFun. eh
U _ usleep
There is no UNIX2003 suffix.
Summary:
My initial goal was to port a library written in C Language (with multiple threads in it) to iOS, compile it with gcc-arch i386, and then start testing, it is found that each call to usleep fails.
At first, I thought it was possible that sleep could not be used, or pthread could not be used. Then I had the experiment above. The conclusion is that the first guess is incorrect, which is a compilation problem, resulting in function discovery during runtime.
The function is not defined.