【发布时间】:2010-11-01 14:36:37
【问题描述】:
我有 n 个文件,我的程序需要使用线程和临时文件将所有文件的内容合并到一个文件中(必须使用 tmpfile())。创建线程时,它必须将 2 个文件合并到一个临时文件(temp1)中,然后另一个线程将下一个 2 个文件合并到另一个临时文件(temp2)等等,然后在下一个级别,另一个线程应该将 temp1 与 temp2 合并到另一个临时文件。
我正在考虑创建一个文件名数组,将其作为参数传递给 pthread_create,该函数也应该返回修改后的数组,但我不知道如何获取临时文件名。会是这样的:
int main(int argc, char *argv[]){
int n = argc -1;
char *files_arr[n];
pthread_t threads[n-1];
}
for (int i=0; i < argc; i++)
{
pthread_create (&threads[i], NULL, temp_merge, (void *) &files_arr);
}
}//end main
void *temp_merge (void *arg){
char *myarray[];
myarray = (char *) arg;
FILE *f1, *f2, *tf;
tf = tmpfile();
//code to merge f1 and f2 into tf, f1 and f2 could be temp files created before
pthread_exit((void*) myarray); //Do I lose the temp file using pthread_exit?
}
问题是:如何访问之前在前一个线程中使用 tmp() 打开的临时文件以生成新的临时文件?
【问题讨论】:
-
"我不知道如何获取临时文件名。" - 你不能,一般来说这就是 tmpfile 的重点
-
没错,Paul,那么我如何“存储”这些临时文件以便以后阅读?
-
创建一个保存名称的数组。在每一步之后,将这些文件合并到新文件中(随时保存这些名称)。这里想到了“递归”这个词;)
-
如果你的意思是“稍后”运行这个或另一个程序:你不能,因为 tmpfile() 完成后删除文件;
-
@Kevin 问题是我无法使用 tmpfile() “保存”临时文件的名称,我认为是递归的,但我认为如果我使用它,它会在一个线程中完成所有操作(我需要一个线程来进行每次合并)。 @Peter 当我说“稍后”时,我的意思是在同一个程序的下一个线程中
标签: c multithreading merge pthreads