linux 多线程的分离和可链接属性
#include "common.h" /** 这种情况一般用于某个多线程调用的模块使用前的初始化,但是无法判定哪个线程先运行,从而不知道把初始化代码放在哪个线程合适的问题。 当然,我们一般的做法是把初始化函数放在main里,创建线程之前来完成,但是如果我们的程序最终不是做成可执行程序,而是编译成库的形式,那么main函数这种方式就没法做到了。 **/ static pthread_t thread_miccapture; static pthread_t thread_audioplay; static int pthread_run = 0; static pthread_t thread_main; static char* pusb_confi ; static float i2c_buf = 0; static sem_t mysem; static int mtd_size[] = { 0x00001000,0x00001000,0x00002000,0x00002000,0x00000c00,0x00002000,0x00000800 }; typedef struct mutex_lock_tag // 后面加tag { pthread_mutex_t mutex; int data; }mutex_lock_t; // 后面加_t //int pthread_once(pthread_once_t *once_control, void (*init_routine) (void)); static pthread_once_t pthread_onces = PTHREAD_ONCE_INIT; static mutex_lock_t* pdata = NULL; static void * mic_capture_thread(void *arg); static void * audio_play_thread(void *arg); static clock_t mic_start = 0; static clock_t audio_play_start = 0; static double time_offset; static unsigned int data = 1; //static void prints(unsigned int data) static void pthread_init(void) { printf("pthread_once init\n"); int ret = sem_init(&mysem,0,1); printf("ret is %d\n",ret); ret = pthread_mutex_init(&pdata->mutex,NULL); // 添加初始化锁 sleep(3); printf("delay delay\n"); // 完成一些需要延时的功能初始化操作 printf("1111122222221\n"); } static void prints(void) { sem_wait(&mysem); if(data<=100) { printf("data is %u\n",data); } else { data = 1; } sem_post(&mysem); } static void * mic_capture_thread(void *arg) { int ret = -1; ret = pthread_equal(thread_main,thread_miccapture); if(ret > 0) { printf("two the same thread\n"); } else { printf("diff in two thread\n"); } //ret = pthread_once(&pthread_onces,pthread_init); printf("ret is %d\n",ret); while(pthread_run) { //printf("mic capture thread\n"); //ret = pthread_mutex_lock(&pdata->mutex); pdata->data = 10; data++; prints(); //ret = pthread_mutex_unlock(&pdata->mutex); //mic_start = clock(); //usleep(100000); } } static void * audio_play_thread(void *arg) { int read_status = 1; int ret = 0; //ret = pthread_once(&pthread_onces,pthread_init); printf("ret is %d\n",ret); while(pthread_run) { // if(read_status) { //if(ret) { //ret = pthread_mutex_lock(&pdata->mutex); //pdata->data = 11; //printf("data.data is %d ret is %d\n",pdata->data,ret); data++; prints(); //ret = pthread_mutex_unlock(&pdata->mutex); //usleep(100000); //audio_play_start = clock(); //time_offset = (double)(audio_play_start - mic_start)/CLOCKS_PER_SEC; //printf("time_offset is %f\n",time_offset); } } } } static int compare(const void* dataA,const void*dataB) { return 0; } void task_create(void) { char* tmp[10] = { 0}; int ret = -1; pthread_run = 1; double totol = 0; int i = 0; pthread_attr_t mic_attr; pthread_attr_t play_attr; int pthread_att; int data[] = {10,200,400,600,700,200}; pdata = malloc(sizeof(mutex_lock_t)); if(pdata == NULL) { printf("pdata is error\n"); } while(i<sizeof(mtd_size)/sizeof(mtd_size[0])) { totol = mtd_size[i++]+totol; } /** int pthread_attr_init(pthread_attr_t *attr); int pthread_attr_destroy(pthread_attr_t *attr); int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate); **/ ret = pthread_attr_init(&mic_attr); printf("ret is %d\n",ret); { ret = pthread_attr_setdetachstate(&mic_attr,PTHREAD_CREATE_DETACHED); printf("ret is %d\n",ret); ret = pthread_create(&thread_miccapture, &mic_attr, // 第二个参数 设置不要是NULL mic_capture_thread, NULL); if(ret<0) { perror("pthread_create failure\n"); return ; } pthread_attr_getdetachstate(&mic_attr,&pthread_att); printf("PTHREAD_CREATE_DETACHED is %d PTHREAD_CREATE_JOINABLE is %d mic pthread_att is is ######%d\n",PTHREAD_CREATE_DETACHED,PTHREAD_CREATE_JOINABLE,pthread_att); pthread_attr_getdetachstate(&play_attr,&pthread_att); printf("play_attr pthread_att is is ######%d\n",pthread_att); ret = pthread_attr_init(&play_attr); printf("ret is %d\n",ret); ret = pthread_attr_setdetachstate(&play_attr,PTHREAD_CREATE_JOINABLE); printf("ret is %d\n",ret); #if 1 ret = pthread_create(&thread_audioplay, &play_attr, // 第二个参数 设置不要是NULL audio_play_thread, NULL); if(ret<0) { perror("pthread_create failure\n"); return ; } #endif pthread_run = 0;// 两个线程退出 ret = pthread_once(&pthread_onces,pthread_init); printf("ret is %d\n",ret); printf("PTHREAD_ONCE_INIT is %d\n",PTHREAD_ONCE_INIT); ret = pthread_join(thread_miccapture,NULL); printf("thread_miccapture join is %d\n",ret); ret = pthread_join(thread_audioplay,NULL); printf("thread_audioplay join is %d\n",ret); pthread_mutex_destroy(&pdata->mutex); sem_destroy(&mysem); pthread_attr_destroy(&mic_attr); // 销毁线程分离|可join属性 pthread_attr_destroy(&play_attr); // 销毁线程分离|可join属性 free(pdata); printf("pthread here\n"); } }
一勤天下无难事。