thpool_t* thpool_init(int threadN) { thpool_t* thpool; if(!threadN || threadN < 1) threadN = 1; ///分配线程池内存 thpool = (thpool_t*) malloc(sizeof(thpool_t)); if(thpool ==NULL) { printf("malloc thpool_t error"); return NULL; } //分配线程数 thpool->threadsN = threadN; thpool->threads =(pthread_t*) malloc(threadN*sizeof(pthread_t)); if(thpool->threads == NULL) { printf("malloc thpool->threads error"); return NULL; } if(thpool_jobqueue_init(thpool)) return -1; thpool->jobqueue->queueSem =(sem_t*)malloc(sizeof(sem_t)); sem_init(thpool->jobqueue->queueSem,0,1); int t; for(t = 0;t< threadN ;t++) { pthread_create(&(thpool->threads[t]),NULL,(void *)thpool_thread_do,(void*)thpool); } return thpool; }
thpool_t * thpool_init (int threadsN){ thpool_t *tp_p ; if (!threadsN || threadsN < 1){ threadsN = 1 ; } tp_p = (thpool_t *)malloc (sizeof (thpool_t)) ; if (tp_p == NULL){ fprintf (stderr ,"thpool_init (): could not allocate memory for thread pool\n"); return NULL ; } tp_p->threads = (pthread_t *)malloc (threadsN * sizeof (pthread_t)); if (tp_p->threads == NULL){ fprintf( stderr , "could not allocation memory for thread id\n"); return NULL; } tp_p->threadsN = threadsN ; if (thpool_jobqueue_init (tp_p) == -1){ fprintf (stderr ,"could not allocate memory for job queue\n"); return NULL; } /*初始化信号*/ tp_p->jobqueue->queueSem = (sem_t *)malloc (sizeof (sem_t)); /*定位一个匿名信号量,第二个参数是1表示。这个信号量将在进程内的线程是共享的,第三个参数是信号量的初始值*/ sem_init (tp_p->jobqueue->queueSem, 0 , 0 ); int t ; for (t = 0 ; t < threadsN ; t++){ printf ("Create thread %d in pool\n", t); //第四个参数是传递给函数指针的一个参数,这个函数指针就是我们所说的线程指针 if (pthread_create (&(tp_p->threads[t]) , NULL , (void *) thpool_thread_do , (void *)tp_p)){ free (tp_p->threads); free (tp_p->jobqueue->queueSem); free (tp_p->jobqueue); free (tp_p); } } return tp_p ; }
/* Initialise thread pool */ thpool_t* thpool_init(int threadsN){ thpool_t* tp_p; if (!threadsN || threadsN<1) threadsN=1; /* Make new thread pool */ tp_p=(thpool_t*)malloc(sizeof(thpool_t)); /* MALLOC thread pool */ if (tp_p==NULL){ fprintf(stderr, "thpool_init(): Could not allocate memory for thread pool\n"); return NULL; } tp_p->threads=(pthread_t*)malloc(threadsN*sizeof(pthread_t)); /* MALLOC thread IDs */ if (tp_p->threads==NULL){ fprintf(stderr, "thpool_init(): Could not allocate memory for thread IDs\n"); return NULL; } tp_p->threadsN=threadsN; /* Initialise each job queue */ tp_p->jobqueue = (thpool_jobqueue **) malloc(tp_p->threadsN * sizeof(thpool_jobqueue *)); int t; printf("tp_p->jobqueue's address is %p\n", tp_p->jobqueue); /* Make threads in pool */ for (t = 0; t < threadsN; t++){ if (thpool_jobqueue_init(& (tp_p->jobqueue[t])) == -1){ fprintf(stderr, "thpool_init(): Could not allocate memory for job queues.\n"); /* MALLOCS INSIDE PTHREAD HERE */ return NULL; } /* printf("tp_p->jobqueue[%d]'s address is %p\n", t, tp_p->jobqueue[t]); */ /* printf("tp_p->jobqueue[%d] points %p\n", t, tp_p->jobqueue[t]); */ } /* Make threads in pool */ for (t=0; t<threadsN; t++){ printf("Created thread %d in pool \n", t); pthread_create(&(tp_p->threads[t]), NULL, (void *)thpool_thread_do, (void *)tp_p->jobqueue[t]); /* MALLOCS INSIDE PTHREAD HERE */ } return tp_p; }
/* Initialise thread pool */ thpool_t* thpool_init(int threadsN){ thpool_t* tp_p; if (!threadsN || threadsN<1) threadsN=1; /* Make new thread pool */ tp_p=(thpool_t*)malloc(sizeof(thpool_t)); /* MALLOC thread pool */ if (tp_p==NULL){ fprintf(stderr, "thpool_init(): Could not allocate memory for thread pool\n"); return NULL; } tp_p->threads=(pthread_t*)malloc(threadsN*sizeof(pthread_t)); /* MALLOC thread IDs */ if (tp_p->threads==NULL){ fprintf(stderr, "thpool_init(): Could not allocate memory for thread IDs\n"); return NULL; } tp_p->threadsN=threadsN; /* Initialise the job queue */ if (thpool_jobqueue_init(tp_p)==-1){ fprintf(stderr, "thpool_init(): Could not allocate memory for job queue\n"); return NULL; } /* Initialise semaphore*/ tp_p->jobqueue->queueSem=(sem_t*)malloc(sizeof(sem_t)); /* MALLOC job queue semaphore */ sem_init(tp_p->jobqueue->queueSem, 0, 0); /* no shared, initial value */ /* Make threads in pool */ int t; for (t=0; t<threadsN; t++){ printf("Created thread %d in pool \n", t); pthread_create(&(tp_p->threads[t]), NULL, (void *)thpool_thread_do, (void *)tp_p); /* MALLOCS INSIDE PTHREAD HERE */ } return tp_p; }