0

I am trying to use pthread in C to compare two strings. The idea is to see if the complete string 2 is in string 1 (For Example, if string1 = lkajdsgl and string2 = jd then I would have one match). What I don't understand is how the pthread works in a general manner. Here I am creating my pthreads, assuming NUM_THREADS=3, then I should have 3 threads, threads[0], threads[1], and threads[2]. Each will call on the Pthrd_Substring function. The strings will be read in from a file and analyzed in the function. However, what I don't understand is how to use pthread_join. If the string is 12 characters long and I only have 3 threads how does the system know to keep analyzing the string with the 3 threads until all 12 characters have been examined? (The function looks at each letter in string 1 and compares it with the first letter in string 2 before determining if the full string 2 is present.)

int main(int argc, char *argv[])
{
    pthread_t threads[NUM_THREADS];
    int count, rc;
    long t;

    for(t=0;t<NUM_THREADS;t++){
     printf("In main: creating thread %ld\n", t);
     rc = pthread_create(&threads[t], NULL, Pthrd_Substring, (void *)t);
     if (rc){
       printf("ERROR; return code from pthread_create() is %d\n", rc);
       exit(-1);
       }
     }

    printf("The number of substrings is: %d\n", count);
    return 1;
}

I can use something like:

pthread_join(threads0, NULL);
 partial_sum += t.partial_count;
pthread_join(threads1, NULL);
 partial_sum += t.partial_count;
pthread_join(threads2, NULL);
 partial_sum += t.partial_count;

And have a global total in the function? But, does this somehow get every letter in the string examined?


I was hesitant to include this part because I haven't worked it all out since I don't understand how the pthread call works exactly in the main. However, this is the pseudo-code I have for the function, here n1 is the string length of string1 and n2 is the lenght of string2:

void *Pthrd_Substring(void *thrdptr)
{
    int i,j,k;
    int count;
    int total = 0;

    for (i = thrdptr; i <= (n1-n2); i++){       
        count=0;
        for(j = i,k = 0; k < n2; j++,k++){  /*search for the next string of size of n2*/  
            if (*(s1+j)!=*(s2+k)){
                break;
            }
            else
                count++;
            if(count==n2)    
                total++;        /*find a substring in this step*/                          
        }
    }
    partial_count = total
}
Joseph
  • 301
  • 3
  • 13
  • 1
    Please show `Pthrd_Substring`. In fact please make sure to provide a [mcve]. – kaylum Apr 24 '17 at 00:17
  • I hope that provides the needed information. The code is not 'complete' because I haven't worked out this one issue. – Joseph Apr 24 '17 at 00:34
  • Who's idea was it to use multiple threads for substring searching? It's as weird an idea as I can think of. If it isn't a somewhat sadistic teacher's view of a homework exercise, then you should avoid doing it. Your `Pthrd_Substring()` function doesn't compile cleanly as shown (`i = thrdptr`). It uses global variables you've not shown (`n1`, `n2`). You have many major problems. You pass a pointer to `t` to all threads, but you've not ensured that each thread sees its own data. You've no idea what each thread is using as the value. – Jonathan Leffler Apr 24 '17 at 00:38
  • By incomplete, do you mean *doesn't compile*? Because `i` is `int`, `thrdptr` is `void*` (thus type mismatch), and the source of `n1`, `n2`, `s1`, `s2`, and `partial_count` are left to the imagination. – WhozCraig Apr 24 '17 at 00:38
  • This is something a professor is having me do to get familiar with pthreads - which I obviously need as I'm missing a fundamental point here. I don't think I said incomplete, but, yea I didn't post the entire thing up because part of it was provided in an example and I just wanted to focus on what I am writing. I suppose I could edit the full program down to what is just mine? – Joseph Apr 24 '17 at 00:43
  • would it be better to re-write this code so it minimizes the issue and post the full code up? – Joseph Apr 24 '17 at 00:53
  • 1
    This is just about the worst possible exercise re. multithreading I've ever seen. Not only is it grossly inappropriate and inefficient to use more than one thread for trivial-size string searches, trying to mangage the searches in the way you describe is nightmarish. – ThingyWotsit Apr 24 '17 at 01:02
  • 1
    If you are going to do this, calculate [num of threads] starting points in the array, roughly equal in size, and have thre threads look for the start char in only their own section. Don't have the work threads read the file - read the fille into an array and load it into a malloced struct, together with the start/length for each thread, and pass the struct address into pthread_create(). Put results into the struct and 'return' the struct with pthread_exit(). Free after pthread_join() and accumulating the results. – ThingyWotsit Apr 24 '17 at 01:07
  • 1
    ..or better, use a thread pool but, given the 'thermonuclear warhead to crack a nut' nature of the assignment, just do it the simpler way and get a few marks. – ThingyWotsit Apr 24 '17 at 01:09

1 Answers1

1

If the string is 12 characters long and I only have 3 threads how does the system know to keep analyzing the string with the 3 threads until all 12 characters have been examined?

The system doesn't know that - just like with non-threaded programming. If you want every character to be analyzed then you need to write your program so it analyzes every character.

However, what I don't understand is how to use pthread_join.

pthread_join just waits for a thread to exit. That's all.

user253751
  • 57,427
  • 7
  • 48
  • 90