Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi all,

I am working with threads in C, I have written a code to create threads which execute a function. Problem is I am getting the threads created, however, the target function is not getting invoked. How do I know it? I have print statements everywhere. I get success as many times as I want the threads but none of the target function print statements are getting printed, which I feel is bizarre.

Below is my code

<<creation of threads>>
Objective-C
void replace(char **args)
{



  char *word1=args[1];
  char *word2=args[2];
  int worker=atoi(args[3]);
  int err;
 // printf("no of workers %d\n",worker);
    int size=0;
      size_t segment;

          int i=0;
            int j;
              //int count=0;
                char *w_ptr;

                         for(i=0;i<worker;i++)
                          {


  int begin = i / (float)worker * filestat.st_size;
  int end   = ( i + 1 )/ (float)worker * filestat.st_size;
   //printf("Searching from %d to %d\n", begin, end );
   thread_data_replace_array[i].begin=begin;
   thread_data_replace_array[i].end=end;
   thread_data_replace_array[i].word1=word1;
   thread_data_replace_array[i].word2=word2;

  //struct thread_data_replace *my_data;
 struct thread_data_replace *my_data=malloc(sizeof(struct thread_data_replace));
   err=pthread_create(&(tid[i]),NULL,s_replace,(void *) &thread_data_replace_array[i]);
    if (err!=0) printf("\n cant create a thread");
     else printf("\n success");
   }

  }


<<>>


<<Target function>>
Objective-C
void *s_replace(void *threadarg)
{
 // int count=0;
  pthread_mutex_lock(&relock);
 printf("\ninside s_replace for a new thread");

  //  struct thread_data_replace *my_data;
 // my_data=malloc(sizeof(struct(thread_data_replace)));
  struct thread_data_replace *my_data=(struct thread_data_replace *)threadarg;

   int begin=my_data->begin;
    int end= my_data->end;
    char *word1= my_data->word1;
   char *word2= my_data->word2;

    int j;
    int i;
    printf("\ninside replace word to be replaced is %s",word1);
    printf("\n inside replace replaced word is %s",word2);
    printf("\n from %d to %d in replace",begin, end);
     for(j=begin;j<end;j++)>
     {
      //printf("\n inside for not inside if replacing \n");
      if(!memcmp(data+j,word1,strlen(word1)))
      {
        printf("inside if this time for replace function replacing words");
          for(i=j;i<strlen(word1);i++)>
          {
           data[i]=word2[i];
          }
      }
      }

    pthread_mutex_unlock(&relock);

 }

<<>>

What I have tried:

I have tried allocating the memory to the struct of arguments while creating the threads instead of in the target function. I have applied locks everywhere so I don't get erratic behavior. If it helps, m working on a unix machine with the help of an ssh.

the output I get when I give 6 threads as input is

success
success
success
success
success
success

and then the program ends
Posted
Updated 13-Apr-16 6:00am
v3
Comments
Sergey Alexandrovich Kryukov 13-Apr-16 12:23pm    
What exactly do you mean by "target function"? Can you point it out in source code by commenting relevant line?
How do you know it wasn't called? Have you tried it under the debugger?
"I applied lock everywhere" sounds like a very bad idea. You need to understand exactly why you use lock. It's not for avoiding "erratic behavior"...
—SA
Shivangi_K 13-Apr-16 12:41pm    
I know it wasn't invoked because none of the print statements worked. and I tried it with and without locks, it gives me the same output.

1 solution

You did not show your main function which calls replace(). Because your programs terminates, you are probably not waiting for the threads to have terminated (done their job). In other words: The main thread reaches it's end and terminates before the worker threads had a chance to be executed.

To avoid this, call pthread_join[^] for all threads at the end of your main() [EDIT: or replace()] function. This will suspend execution of the main thread until the joined worker threads had terminated.
 
Share this answer
 
v2
Comments
Shivangi_K 13-Apr-16 12:43pm    
yes that makes sense, pthread_join fixed the problem, I wasn't aware, that the main thread could finish execution before all the threads could execute. An important lesson learnt. thanks!
Shivangi_K 13-Apr-16 12:45pm    
however, now, the even after the execution of all threads the main doesn not exit, and I have to give an Ctrl C to break it
Jochen Arndt 13-Apr-16 12:52pm    
Then you might call pthread_join for a thread that is blocked (for example by a mutex). You should debug your application (or use poor man's debugging by printing states) to see what happens.

By the way: Why do you lock the whole thread function?
If the mutex (relock) is global (the same for all threads), they will not be executed in parallel but one after one making the usage of threads useless.
Sergey Alexandrovich Kryukov 13-Apr-16 12:51pm    
5ed; that was my guess...
—SA
Shivangi_K 13-Apr-16 12:54pm    
I removed the mutex lock to see if that is causing all the trouble, it still gets stuck on the n-1th thread for n threads and I have to give ctrl C command

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900