0

I'm making a program where I have multiple threads working at the same time. After messing with the code for a while, I had an issue where I would send a string (char *) to the thread to do further operations with, and somehow the string did not send at all.

Later, I wrote a very simple code where I just send a string to a thread and the function prints it to the console. I found out that without using pthread_join(), it wouldn't work at all. I have no idea why, because I know that whenever pthread_join() is called, it blocks every other thread and waits until that one is finished.

Here's the simple program:

void* prisntstr(void* string);

int main(int argc, char *argv[])
{
    char* string = "Hello!";

    pthread_t thread;

    pthread_create(&thread, NULL, prisntstr, (void*)string);

    pthread_join(thread, NULL);

}

void* prisntstr(void* string)
{
    char* str = (char*)string;

    printf("%s\n", str);
}

Does anyone know how I can do this without using pthread_join()?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
michael368
  • 43
  • 3

3 Answers3

2

The problem with your program, if you remove the pthread_join, is that main is returning and thereby causing the program to exit without doing anything to synchronize with the other thread and determine whether it's actually finished. When main returns, it's just as if you called exit, and the whole process terminates.

Aside from pthread_join there are lots of ways you could make main wait for other actions to be finished. You could wait on a semaphore that the other threads post, you could loop on a condition variable inspecting data other threads set, etc.

Alternatively you could have main call pthread_exit so to terminate the initial thread. Then the process will not exit until you call exit or until each thread has exited.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
1

The thread calling pthread_join() just waits for another thread to finish. Please note that:

If that thread has already terminated, then pthread_join() returns immediately

It is the most elegant way for doing it, being the others more complicated, and involving IPC tecniques:

  • the calling thread could wait for a mutex/semaphore put by the secondary thread
  • the calling thread could wait for a signal sent by the secondary thread
  • ... and so on

So basically the strategy is: synchronize threads so that

  1. The main thread can obtain some information calculated by the child thread
  2. The process is kept alive until the child thread has completed its action (like in this case)

The reason why without pthread_join() you dont see that message printed to stdout is that as soon as main terminate, it terminates the whole process and all children threads are terminated; in your case before the print is executed.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • `sleep()` does not belong in this discussion, unless to warn against trying to use it for such purposes. – John Bollinger May 29 '20 at 21:40
  • @JohnBollinger it is not an advice. It was an explanation (apparently bad worded) about _giving enough time to the secondary thread to finish. I'll try to reword – Roberto Caboni May 29 '20 at 21:44
  • @JohnBollinger I ended up removing the reference to sleep. It is a shame because it would have been a good "didactic" way to show how it was a matter of time. But it has been a long day and I cannot find the words to explain it. – Roberto Caboni May 29 '20 at 21:58
0
  • When you use thread in your program, created thread will became child thread and the main program will became main thread. So, we need to block the main thread so that it can not close.

  • If main thread closed then child threads will be exit as well. So, You need to find the way how to block main thread to not exit. thread gives us the facility for that is pthread_join(). This will block the main thread until the child thread is working. Main thread will block on pthread_join() line. It will not execute other lines.

  • If you want to not use pthread_join() you need to find other ways for that.

Ex:

while(1);
  • while(1); will not end the main thread until you kill the process.