쓰레드 폴 생성시 개수를 define에 정의 하지 않고 설정파일에서
글쓴이: ljksky / 작성시간: 금, 2003/04/18 - 1:46오후
쓰레드 폴 생성시 개수를 define에 정의 하면 구조체들을
Threads thread_pool[NUMBER_OF_THREADS];
pthread_mutex_t mutex[NUMBER_OF_THREADS];
pthread_cond_t cond[NUMBER_OF_THREADS];
이렇게 선언하면 되는데..
define 하지 않고 설정파일에서 읽어 올때에는
어떻게 선언하고 사용해야하는지 모르겠습니다.
. . . #define INI_FILE_NAME "./dbcli.conf" #define NUMBER_OF_THREADS 5 typedef struct thr_info { pthread_t tid; int client_fd; int client_port; int is_enabled; char client_ip[16]; char data[200]; } Threads; int thread_cnt; Threads thread_pool[NUMBER_OF_THREADS]; pthread_mutex_t mutex[NUMBER_OF_THREADS]; pthread_cond_t cond[NUMBER_OF_THREADS]; struct sockaddr_in clientaddr[NUMBER_OF_THREADS]; int initThreads(){ int i, ret = 0; char print_buf[512]; pthread_t thread_id; for(i = 0; i < NUMBER_OF_THREADS; i++){ pthread_mutex_init(&mutex[i], NULL); pthread_cond_init(&cond[i], NULL); } for(i = 0; i < NUMBER_OF_THREADS; i++){ thread_pool[i].client_fd = -1; thread_pool[i].is_enabled = 0; ret = pthread_create(&thread_pool[i].tid, NULL,processClientRequest, &thread_pool[i].tid); if(ret != 0) { perror("pthread"); sprintf(print_buf,"pthread_create ERROR [%d]\n", ret); Print(print_buf); log_write(print_buf); return 1; } } return 0; } void *processClientRequest(void *data) { pthread_t *tid = (pthread_t *)data; long sec, usec; for(index = 0; index < NUMBER_OF_THREADS; index++) { if(thread_pool[index].tid == *tid) { sprintf(print_buf, "THREAD INDEX .. [%d]...\n", index); Print(print_buf); log_write(print_buf); break; } } for(;;){ if(thread_pool[index].is_enabled == 0) { pthread_mutex_lock(&mutex[index]); pthread_cond_wait(&cond[index], &mutex[index]); /*continue;*/ } /** 작업.. 루틴.. **/ thread_pool[index].is_enabled = 0; sprintf(print_buf, "**** thread_pool[index].is_enabled = [%d]\n", thread_pool[index].is_enabled); Print(print_buf); pthread_mutex_unlock(&mutex[index]); } } void Thread_pool(int client_sockfd, char *data_buf){ int i; char mprint_buf[512]; for(i = 0; i < NUMBER_OF_THREADS; i++) { if(thread_pool[i].is_enabled == 0) { sprintf(mprint_buf, "**** CHECK IN thread...number [%d] \n", i); Print(mprint_buf); pthread_mutex_unlock(&mutex[i]); pthread_mutex_lock(&mutex[i]); thread_pool[i].client_fd = client_sockfd; thread_pool[i].is_enabled = 1; thread_pool[i].client_port = ntohs(clientaddr.sin_port); strncpy(thread_pool[i].client_ip, inet_ntoa(clientaddr.sin_addr), strlen(inet_ntoa(clientad dr.sin_addr))); strncpy(thread_pool[i].data, data_buf, strlen(data_buf)); pthread_cond_signal(&cond[i]); pthread_mutex_unlock(&mutex[i]); break; } } } int main(int argc, char *argv[]){ int server_sockfd; int client_len; int pid, i; int count=0; int read_size=0; char mprint_buf[512]; pthread_t chld_thr; char read_buf[200]; char szBuff[200]; read_config(); //설정파일을 읽어 생성 쓰레드 개수를 thread_cnt에 담는다 if(initThreads() != 0) { log_write("Cannot initialize threads...\n"); fprintf(stderr, "Cannot initialize threads...\n"); return 1; } while(1){ memset(read_buf, 0x00, sizeof(read_buf)); read_size = read(server_sockfd, read_buf, sizeof(read_buf)); Thread_pool(server_sockfd, read_buf); } }
Forums:
Threads thread_pool[NUMBER_OF_THREADS];
Threads thread_pool[NUMBER_OF_THREADS];
pthread_mutex_t mutex[NUMBER_OF_THREADS];
pthread_cond_t cond[NUMBER_OF_THREADS];
를
Threads *thread_pool;
pthread_mutex_t *mutex;
pthread_cond_t *cond;
이렇게 선언하고
설정에서 스레드 개수를 읽은뒤에 alloc 해서 사용하면 될꺼 같은데요
댓글 달기