Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multithreading - slower than single threading

When I run my program with multiple threads instead of single thread it gets slower, isn't it supposed to be faster? The program is supposed to go through all directories starting from a start directory and find and print all files named X. Heres the code:

while(!done) {
        pthread_mutex_lock(&lock);
        if(!list_isEmpty(dirList)) {
            DIR *dir;
            struct dirent *d;
            char *folder;

            folder = strdup(list_inspect(1, dirList));
            list_remove(list_inspect(1,dirList), dirList);
            if(folder == NULL) {
                perror("failed strdup on path\n");
                pthread_mutex_unlock(&lock);
                continue;
            }
            pthread_mutex_unlock(&lock);
            dir = opendir(folder);
            if(dir == NULL) {
                perror(folder);
                free(folder);
                continue;
            }
            while ((d = readdir(dir)) != NULL) {
                if(strcmp(d->d_name, ".")==0 || strcmp(d->d_name, "..")==0) {
                    continue;
                }
                searchBasedOnType(folder, info, d);
            }
            closedir(dir);
            free(folder);
        }
        else {
            if(sleepCounter == info->nrthr-1) {
                done = true;
                pthread_cond_broadcast(&cond);
                pthread_mutex_unlock(&lock);
                break;
            }
            else {
                sleepCounter++;
                pthread_cond_wait(&cond, &lock);
                sleepCounter--;
                pthread_mutex_unlock(&lock);                
            }
        }   
    }

Here is also an example of the function searchBasedOnTypes call functions with mutex etc:

char currentPath[FILENAME_MAX];
        struct stat buf;

        strcpy(currentPath,f);
        if(currentPath[strlen(currentPath)-1] != '/') {
            strcat(currentPath, "/");
        }
        strcat(currentPath, d->d_name);
        if(lstat(currentPath, &buf) == -1){
          perror(currentPath);
          return;
        }

        if(S_ISDIR(buf.st_mode)) {
            pthread_mutex_lock(&lock);
            char *newDir = malloc(sizeof(currentPath));
            if(newDir == NULL) {
                perror("Failed allocating memory for path\n");
                pthread_mutex_unlock(&lock);
                return;
            }
            strcpy(newDir, currentPath);
            printf("insert %s\n", newDir);
            list_insert(newDir, dirList);
            printf("done\n");
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&lock);
        }
        if(strcmp(name,d->d_name) == 0) {
            printf("%s\n",currentPath);
        }

Can somebody help me find what might slow the program down when this is ran with multiple threads and maybe a solution? Thank you for all help and advice in advance!

like image 503
Frans Avatar asked Oct 26 '25 18:10

Frans


1 Answers

People often things threads are the computational equivalence of bacon (makes everything better).

In reality: Multiple threads don't always speed things up. Multiple threads usually only speed things up if your threads are accessing different hardware resources that can be serviced in parallel. Two threads running CPU-bound tasks on two different cores that don't share memory (or rarely share memory) will probably be faster. If they're sharing memory or hitting the same disk, there's a good chance they're going to contend.

Since the disk is by far the slowest resource that you're program is dealing with, I'm not at all surprised that you aren't seeing any speedup. Try the same experiment with multiple disks (one thread per disk), and you'll probably see an improvement.

Also, the more synchronization your program has (mutex locks) the less parallelism you will usually have. So, performance will suffer further.

like image 149
J Sorber Avatar answered Oct 29 '25 08:10

J Sorber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!