0
#include <pthread.h>
#include <stdio.h>

typedef struct thread_char_para {
    char character;
    int count;
} thread_char_para;

void* char_print (void* parameter)
{
    thread_char_para* p = (thread_char_para*)parameter;
    int i;
    for (i = 0; i < p->count; ++i)
        fputc(p->character, stderr);
    return NULL;
}
int main()
{
    pthread_t thread1;
    pthread_t thread2;
    thread_char_para para1 = {'x', 30000};
    thread_char_para para2 = {'o', 30000};

    pthread_create(&thread1, NULL, char_print, &para1);
    pthread_create(&thread2, NULL, char_print, &para2);

    return 0;
}

Why there is no any output?

I alse find some links to read: Detached vs. Joinable POSIX threads

in this link, it says pthread_join is not necessary. So, i am wondering.

Community
  • 1
  • 1
BlackMamba
  • 10,054
  • 7
  • 44
  • 67
  • Without using `pthread_join` the program probably exits before the threads run – Galik Jun 29 '16 at 06:28
  • Looking into your history you got the answer as well with [your previous question](http://stackoverflow.com/questions/38090256/is-pthread-join-a-must-when-using-pthread-in-linux) – LPs Jun 29 '16 at 07:31

4 Answers4

3

You need to call pthread_join()or pthread_exit() after pthread_create(), otherwise the main() will return killing the spawned threads as well. So no output will get print.

Twinkle
  • 514
  • 3
  • 8
  • 1
    Also if you call pthread_exit() from main(), it will allow the spawned threads to continue execution, terminating only the main thread. If you end main with pthread_exit() your are saying explicitly that you want the other threads to continue. – Twinkle Jun 29 '16 at 06:50
  • when the main thread is killed by `pthread_exit`, what about the process which own the main thread? is it killed? – BlackMamba Jun 29 '16 at 07:12
  • @BlackMamba: `pthread_exit()` does not "kill" a thread, it exits it in a well defined manner. – alk Jun 29 '16 at 09:12
2

You should add pthread_join calls to ensure your main function waits for the other threads to complete:

int main()
{
    pthread_t thread1;
    pthread_t thread2;
    thread_char_para para1 = {'x', 30000};
    thread_char_para para2 = {'o', 30000};

    pthread_create(&thread1, NULL, char_print, &para1);
    pthread_create(&thread2, NULL, char_print, &para2);

    pthread_join(thread1, NULL);  // <--\ add these
    pthread_join(thread2, NULL);  // <--/   two lines.

    return 0;
}

To give you an example on when a pthread_join can be avoided take a look at the followin code

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

typedef struct thread_char_para {
    char character;
    int count;
} thread_char_para;

void* char_print (void* parameter)
{
    thread_char_para* p = (thread_char_para*)parameter;
    int i;

    if (p->character == 'x')
    {
        pthread_t thread2;
        thread_char_para para2 = {'o', 30000};
        pthread_create(&thread2, NULL, char_print, &para2);

        for (i = 0; i < 5; ++i)
        {
            sleep(1);
        }
    }
    else
    {
        for (i = 0; i < p->count; ++i)
            fputc(p->character, stderr);
    }
    return NULL;
}
int main()
{
    pthread_t thread1;
    thread_char_para para1 = {'x', 30000};

    pthread_create(&thread1, NULL, char_print, &para1);

    pthread_join(thread1, NULL);

    return 0;
}

As you can see main launches pthread1 and waits for pthread1 completion. pthread1 starts another thread2 an loops for 5 seconds while the pthread2 is doing its job. In this case pthread_join is not used.

LPs
  • 16,045
  • 8
  • 30
  • 61
  • http://stackoverflow.com/questions/3756882/detached-vs-joinable-posix-threads, in this link, it says `pthread_join` is not necessary. – BlackMamba Jun 29 '16 at 06:36
  • 2
    Here it is needed, because when the main() returns the application will close. So it has to wait till the threads complete execution – Twinkle Jun 29 '16 at 06:39
  • As already stated by @Twinkle when the main finish, the application is close and terminates all threads. – LPs Jun 29 '16 at 06:44
  • 1
    Also if you call pthread_exit() from main, it will allow the spawned threads to continue execution, terminating only the main thread. If you end main with pthread_exit() your are saying explicitly that you want the other threads to continue. – Twinkle Jun 29 '16 at 06:50
  • @Twinkle I want to know, which situation is needed, which is not needed? i am very confused. – BlackMamba Jun 29 '16 at 06:56
  • Basically it is always necessary into the main function that launch the first thread. Could not be necessary if you launch a `thread2` within another `thread1` that was launched by main that is blocked by `pthread_join(thread1)` function. – LPs Jun 29 '16 at 07:03
  • @BlackMamba. You need either pthread_join() or pthread_exit() for the spawned threads to complete execution. If you are using pthread_exit(), it will terminate the main thread allowing the spawned ones to continue execution. If you use pthread_join(), main thread will wait till the spawned threads complete execution. If you use neither of this entire application will close once the main returns. – Twinkle Jun 29 '16 at 07:06
2

As an alternative to joining the two threads one could treat the main()-thread like any other thread and have it exit using pthread_exit().

Doing so would prevent the process along with all its threads from being ended the moment main() returns.

alk
  • 69,737
  • 10
  • 105
  • 255
1

Returning from the main function is equivalent to calling exit. It kills the process and all its threads. So your program kills the threads before they have a chance to run. pthread_create doesn't mean that the thread executes inside pthread_create, it means that the thread will execute at some point in the future and the first thing you do after creating the thread is to kill them which makes it more than likely that they haven't gotten any chance to start running yet.

You can solve this in various ways. As others have mentioned you can wait until the threads have finished running with pthread_join, you can have the threads indicate to main that they are done through a pipe, semaphore, barrier, condition variables and dozens of other ways. You just have to make sure that main can't return until the threads are done otherwise you'll most likely just kill them before they had a chance to run.

pthread_join is not necessary as the answer you refer to says. It's just the easiest way in your case to wait for the threads to finish running. Strictly speaking, you don't even need to wait for the threads to finish running, you just need them to indicate to the main thread that they are done doing what you want them to do and the threads finishing is the easiest way to know that they have started at all.

Art
  • 19,807
  • 1
  • 34
  • 60