3

Now this is just a little test, and part of a school assignment. In my code printf is not printing at least to me being able to see it. Is this a result of the thread not functioning? The print line works outside of the thread. Thank you for any help.

I am new to threading in c.

#include<stdio.h>
#include<pthread.h>
#include<string.h>
#include<stdlib.h>


void *threadServer(void *arg)
{
        printf("This is the  file Name: %s\n", arg);
        pthread_exit(0);


}

int main(int argc, char* argv[]){
        int i=1;
        while(argv[i]!=NULL){
                pthread_t thread;
                pthread_create(&thread, NULL, threadServer,argv[i]);
                i++;
        }
TheMangaStand
  • 193
  • 3
  • 13

1 Answers1

6

In your code, the parent thread of execution that created another thread finishes execution without waiting for its child threads to finish. And threads, unlike processes, once the parent thread terminates, all its child threads of execution terminate as well.

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>

void *threadServer(void *arg)
{
    printf("This is the  file Name: %s\n", (char*)arg);
    pthread_exit(0);
}

int main(int argc, char* argv[]){
    int i=1;
    while(argv[i]!=NULL){
        pthread_t thread;
        pthread_create(&thread, NULL, threadServer, argv[i]);
        i++;
        pthread_join(thread, NULL);
    }
}

Doing this will allow the thread created to run, until it finishes execution. The pthread_join will wait for the thread to complete its execution and then move ahead.

EDIT

As people did mention in the comments, it is probably worthless trying to spawn a single thread and joining it immediately, making it no better than a single thread of execution. Hence, for the sake of experimentation, the code can be modified as follows:

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>

void *threadServer(void *arg)
{
    printf("This is the  file Name: %s\n", (char*)arg);
}

int main(int argc, char* argv[]){
    int i = 1;
    pthread_t thread[argc - 1];

    while(i < argc)
    {
        pthread_create(&thread[i-1], NULL, threadServer, argv[i]);
        i++;
    }

    for (i = 0; i < argc - 1; ++i)
    {
        pthread_join(thread[i], NULL);
    }
}
John Strood
  • 1,859
  • 3
  • 26
  • 39
  • Of course, spawning a thread and then immediately joining with it is a somewhat strange thing to do, beyond an academic exercise. – pat Nov 26 '16 at 19:18
  • @pat Strange, yes. But only to suit the OP's academic experiments. It is not advisable to go with this, since it makes no sense to create a single thread, and later join it, and spawn another again! Since it is then no better than using a single thread of execution. This is still worse, considering all the overheads involved in creating those threads. – John Strood Nov 26 '16 at 19:29
  • @Djack oh so it won't be concurrent. Then joining it after the loop will? If I were to understand this currectley. – TheMangaStand Nov 26 '16 at 19:33
  • @TheMangaStand All threads run concurrently, unless you were running this on a multiprocessor machine. In this case, the order is well defined, since it is equivalent to using a single thread. But, if you were to create multiple threads at once, then the order cannot be predicted (not synchronized). – John Strood Nov 26 '16 at 19:39
  • yea okay I get this a lot more then when we were thrown into a threading assignment a couple weeks ago. Thanks for your help. I do want multiple threads at once and I'm not worried about the order for the thing I am going to do. – TheMangaStand Nov 26 '16 at 19:44