Static pthread_mutex_t dbMutex = PTHREAD_MUTEX_INITIALIZER;
Static sem_t * dbSem [10];
Static sqlite3 * db;
Static sqlite3_stmt * insertStmt = NULL;
Static sqlite3_stmt * queryStmt = NULL;
Void createAndOpenDB (void );
Void * insertIntoDB (void *);
Void queryFromDB (void );
Void readFromDB ();
Void createInsertThread (sem_t * sem );
Void queryFromDB (void)
{
// Sem_t * thisSem = (sem_t *) info;
Pthread_mutex_lock (& dbMutex );
If (queryStmt = NULL ){
NSString * selectSQL = @ "select count (*) from contacts ";
If (sqlite3_prepare_v2 (db, [selectSQL cStringUsingEncoding: NSUTF8StringEncoding],-1, & queryStmt, NULL )! = SQLITE_ OK)
@ Throw [NSException exceptionWithName: @ "query error" reason: nil userInfo: nil];
}
If (sqlite3_step (queryStmt )! = SQLITE_ROW)
@ Throw [NSException exceptionWithName: @ "query error" reason: nil userInfo: nil];
Int count = sqlite3_column_int (queryStmt, 0 );
NSLog (@ "% d \ n", count );
Sqlite3_reset (queryStmt );
Pthread_mutex_unlock (& dbMutex );
// Sem_post (thisSem );
}
Void * insertIntoDB (void * info)
{
NSLog (@ "start ");
NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
// Pthread_cond_t * thisCond = (pthread_cond_t *) info;
Sem_t * thisSem = (sem_t *) info;
Pthread_mutex_lock (& dbMutex );
Sqlite3_exec (db, [@ "begin transaction" cStringUsingEncoding: NSUTF8StringEncoding], NULL, NULL );
NSString * insertSQL = @ "insert into contacts values (NULL ,?,?) ";
If (insertStmt = NULL ){
If (sqlite3_prepare_v2 (db, [insertSQL cStringUsingEncoding: NSUTF8StringEncoding],-1, & insertStmt, NULL )! = SQLITE_ OK ){
@ Throw [NSException exceptionWithName: @ "prepare statement error" reason: nil userInfo: nil];
}
}
NSString * name = @ "eeeyes ";
Const char * nameBytes = [name cStringUsingEncoding: NSUTF8StringEncoding];
NSString * telphone = @ "1234 ";
Const char * telphoneBytes = [telphone cStringUsingEncoding: NSUTF8StringEncoding];
For (int I = 0; I <10000; I ++ ){
Sqlite3_bind_text (insertStmt, 1, nameBytes,-1, SQLITE_STATIC );
Sqlite3_bind_text (insertStmt, 2, telphoneBytes,-1, SQLITE_STATIC );
If (sqlite3_step (insertStmt )! = SQLITE_DONE ){
NSLog (@ "insert error: % s", sqlite3_errmsg (db ));
Sqlite3_close (db );
@ Throw [NSException exceptionWithName: @ "insert error" reason: nil userInfo: nil];
}
Sqlite3_reset (insertStmt );
}
Sqlite3_exec (db, [@ "commit" cStringUsingEncoding: NSUTF8StringEncoding], NULL, NULL );
// Sleep (20 );
// Pthread_cond_signal (thisCond );
Sem_post (thisSem );
Pthread_mutex_unlock (& dbMutex );
// [Pool drain];
[Pool release];
NSLog (@ "end ");
}
Void createInsertThread (sem_t * sem)
{
Pthread_t threadID;
// Pthread_create (NULL, NULL, insertIntoDB, NULL );
Pthread_create (& threadID, NULL, insertIntoDB, sem );
}
Void createAndOpenDB (void)
{
// Init wait condition
For (int I = 0; I <10; I ++)
DbSem [I] = sem_open ([[NSString stringWithFormat: @ "% d", I] cStringUsingEncoding: NSASCIIStringEncoding], O_CREAT, S_IRUSR, 0 );
// Create database
Sqlite3_config (SQLITE_CONFIG_SINGLETHREAD); // single thread module
If (sqlite3_open ([@ "/eeeyes. db" cStringUsingEncoding: NSUTF8StringEncoding], & db )! = SQLITE_ OK ){
Sqlite3_close (db );
@ Throw [NSException exceptionWithName: @ "create db fail" reason: nil userInfo: nil];
}
// Create table
NSString * createTableSQL = @ "create table if not exists contacts (id integer primay key autoincreament ,"
"Name text, telphone text )";
Char * erroMessage;
If (sqlite3_exec (db, [createTableSQL cStringUsingEncoding: NSUTF8StringEncoding], NULL, NULL, & erroMessage )! = SQLITE_ OK ){
NSLog (@ "create table fail: % s", erroMessage );
@ Throw [NSException exceptionWithName: @ "create table fail" reason: nil userInfo: nil];
}
}
@ Implementation AppDelegate
-(Void) dealloc
{
[_ Window release];
[Super dealloc];
}
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
Clock_t start, end;
CreateAndOpenDB ();
Start = clock ();
For (int I = 0; I <10; I ++)
CreateInsertThread (* (dbSem + I ));
// Sleep (10 );
For (int I = 0; I <10; I ++ ){
Sem_wait (* (dbSem + I ));
}
QueryFromDB ();
End = clock ();
Printf ("total seconds: % f \ n", (double) (end-start)/CLOCKS_PER_SEC );
Self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease];
// Override point for customization after application launch.
Self. window. backgroundColor = [UIColor whiteColor];
[Self. window makeKeyAndVisible];
Return YES;
}
@ End
It seems that ios does not have good support for unknown semaphores, So it uses famous semaphores.
Connecting the past and the future.
From eeeyes