【发布时间】:2019-04-26 12:23:29
【问题描述】:
我一直在进行多线程目录搜索,每次运行到新目录或文件时都会创建一个线程。到目前为止,文件线程是并行的并且工作正常,但我不确定在哪里加入新的目录线程,因为它是递归的。我试过把它放在函数的底部,但它只是创建了一个循环(不是无限的,但肯定是错误的)
我的代码在下面,为了便于理解,我还有一个伪代码。
为了便于解释和清理杂乱的代码,这里是一个伪代码:
recursive function{
if (!(dir = opendir(ca->SD))){{
return;
}
while ((ptr = readdir(dir)) != NULL) {
if (ptr->d_type == DT_DIR) {
if (strcmp(ptr->d_name, "..") == 0||strcmp(ptr->d_name, ".") == 0){
continue;
}
create thread/call recursive function
}else(if file){
create thread/call file handler function
}
}
thread join for any live threads;
}
我只是对在哪里正确放置任何目录线程的连接感到困惑。我目前一直在循环。
【问题讨论】:
-
pthread_mutex_lock(&mutex);thenif (pthread_self() == initialtid){...在if关闭后没有pthread_mutex_unlock(),但在if语句中多次调用pthread_mutex_unlock()本身就是灾难的根源。
标签: c multithreading recursion directory thread-safety