thread* thread_new(int32_t flag, void *(*routine)(void*), void *ud) { pthread_attr_t attr; pthread_attr_init(&attr); if(flag | JOINABLE) pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE); else pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); thread *t = calloc(1,sizeof(*t)); struct start_arg starg; starg.routine = routine; starg.arg = ud; starg.running = 0; starg.mtx = mutex_new(); starg.cond = condition_new(starg.mtx); pthread_create(&t->threadid,&attr,start_routine,&starg); mutex_lock(starg.mtx); while(!starg.running){ condition_wait(starg.cond); } mutex_unlock(starg.mtx); condition_del(starg.cond); mutex_del(starg.mtx); pthread_attr_destroy(&attr); return t; }
static Link addChild(Link self,Link value,Link keys){ Link * args = array_getArray(keys); size_t argn = array_getLength(keys); if (argn != 1) return NULL; string_t key = object_getString(args[0]); if ( ! key ) return NULL; Dict dict = self->value.vptr; if ( ! dict){ dict = self->value.vptr = malloc( sizeof( *dict) ); dict->dictionary = dictionary_new(); dict->mutex = mutex_new(); } mutex_lock(dict->mutex); Link old = dictionary_insert(dict->dictionary, key, value ); mutex_unlock(dict->mutex); if (old) link_free(old); return value; // return original not duplicate child }
int init_zlog(const char *file_prefix, unsigned int file_max_size) { if (file_prefix == NULL || file_max_size == 0) { printf("illegal param.\n"); return S_ERROR; } memset(&gs_zlog, 0, sizeof(gs_zlog)); if (new_copy_str(&gs_zlog.file_prefix, file_prefix, NULL) != S_OK) { printf("fail to copy file_prefix.\n"); return S_ERROR; } gs_zlog.file_max_size = file_max_size; gs_zlog.is_to_exit = FALSE; if (thread_new(&gs_zlog.thread) != S_OK || mutex_new(&gs_zlog.mutex) != S_OK || sema_new(&gs_zlog.sema, 0) != S_OK) { printf("fail to new thread.\n"); return S_ERROR; } if (thread_start(gs_zlog.thread, _thread_zlog_do, NULL) != S_OK) { printf("fail to start thread.\n"); return S_ERROR; } return S_OK; }
int main(void){ NumPrinter *p1, *p2, *p3, *p4, *p5; mutex_t * mut = mutex_new(); CONSTR(mut); p1 = num_pr_new(1); p2 = num_pr_new(2); p3 = num_pr_new(3); p4 = num_pr_new(4); p5 = num_pr_new(5); printStart(p1); printStart(p2); printStart(p3); printStart(p4); printStart(p5); getchar(); printer_free(p1); printer_free(p2); printer_free(p3); printer_free(p4); printer_free(p5); DESTR(); }
int main() { int i = 0; int old_recv_len = 0; for(; i < MAX_CLIENT_NUM; ++i) { index_array[i] = -1; } index_num = 0; m = mutex_new(); all_recved_lock = mutex_new(); test(); while(true) { thread_sleep(1000); printf("all_recved is %dk/s\n", (all_recved-old_recv_len)); old_recv_len = all_recved; } return 0; }
int cond_new(cond_t *cond) { int rc; if (cond == NULL) return EINVAL; rc = pthread_cond_init(&cond->id, NULL); if (rc) return rc; rc = mutex_new(&cond->lock); if (rc) pthread_cond_destroy(&cond->id); return rc; }
int main() { mutex_t * mutex = mutex_new(); Printer_t printers[3]; for(int i = 0; i < 3; i++){ char str[16]; printers[i] = Printer_new(itoa(i + 1, str, 10), mutex); } for(int i = 0; i < 3; i++){ Printer_startPrintLoop(printers[i]); } for(int i = 0; i < 3; i++){ Printer_join(printers[i]); } for(int i = 0; i < 3; i++){ Printer_delete(printers[i]); } mutex_free(mutex); return 0; }
int rw_mutex_new(rw_mutex_t *lock) { int rc; rc = mutex_new(&lock->lock); if (rc) return rc; rc = pthread_cond_init(&lock->read_sig, NULL); if (rc) { mutex_delete(&lock->lock); return rc; } rc = pthread_cond_init(&lock->write_sig, NULL); if (rc) { mutex_delete(&lock->lock); pthread_cond_destroy(&lock->read_sig); return rc; } lock->state = 0; lock->blocked_writers = 0; return 0; }
static NATIVECALL(mutex_new_native){ Link link = object_create(mutex_type); link->value.vptr = mutex_new(); return link; }
numbers_t* numbers_create(void){ numbers_t* numbers = (numbers_t*)malloc(sizeof(struct numbers_s)); numbers->position = 0; numbers->mutex = mutex_new(); return numbers; }