【发布时间】:2019-07-15 20:08:00
【问题描述】:
在我的 Advance OS 工作中,我需要从文件中读取数据并将其分配给线程以进行进一步处理..
这是我从文件中读取数据并将其传递给线程函数的代码
int main() {
FILE *fp = fopen(fileName, "r");
char str_pass[80][MAX_CHAR];
if (fp == NULL){
printf("Could not open file");
return 1;
}
int i=0;
pthread_t thread[THREADS];
int rc;
for(int th=1; th<=THREADS; th++)
{
if(fgets(str_pass[i], MAX_CHAR, fp) != NULL){
//printf("Hello, Thread %d\n",i);
rc = pthread_create(&thread[i], NULL, create_thread, (void*)str_pass[i]);
pthread_join(thread[i],NULL);
if(rc)
{
printf("ERROR; return code from pthread_create() is %d\n",rc);
exit(-1);
}
pthread_join(thread[i],NULL);
i++;
}
else{
printf("End of File");
exit(0);
}
}
pthread_exit(NULL);
fclose(fp);
return 0;
}
这是我的线程代码;
void * create_thread(void *hash_string)
{
gen_combinations(hash_string);
//sleep(1);
pthread_exit(NULL);
return NULL;
}
这段代码运行良好,它创建的线程与我在THREADS 变量中定义的值一样多,除非它在文件中找不到任何记录。但现在我必须用线程池概念来做这件事。因为我无法生成与文件中的数据一样多的线程。
所以我需要使用线程池来实现多线程。我做了 对其进行了一些搜索,但没有获得任何许可。而现在我 完全卡在这里,不知道从哪里开始, 这个工作怎么做???
我们将不胜感激。
【问题讨论】:
标签: c multithreading operating-system threadpool