2

I am wondering, what can happen if we do a pthread_create without a pthread_join?

Who will "clean" all the memory of the "non-joined" thread.

alk
  • 69,737
  • 10
  • 105
  • 255
user3242743
  • 1,751
  • 5
  • 22
  • 32
  • 1
    See this question, http://stackoverflow.com/questions/22798068/making-threads-fails-after-a-while. The original code in the question calls `pthread_create()` repeatedly without performing a join. `pthread_create()` eventually fails with out of memory. – esorton Apr 11 '14 at 16:52

3 Answers3

1

When the process terminates, all resources associated with the process cease to exist. (This of course does not include shared resources the process created, like files in the filesystem, shared memory segments, etc.) Until then, unjoined threads will continue to consume resources, potentially calling future calls to pthread_create or even malloc to fail.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Why would those calls fail just because a thread is not yet terminated? – Martin James Apr 11 '14 at 16:12
  • Because these things take resources. A thread needs a few megabytes of address space for its stack mapping, for example, and when you run out of address space, you cannot start a new thread. – Simon Richter Apr 11 '14 at 16:16
  • It's very common to create threads at app startup and never terminate them, eg. app-lifetime threads and pool threads. Such use does not necessarily require join(), and would not continually use up any extra resources. – Martin James Apr 11 '14 at 16:34
  • 1
    @MartinJames: The same applies to other resources like memory. There's nothing fundamentally harmful about holding a resource for the full process lifetime as long as the amount of resources held is bounded and unobtrusive. – R.. GitHub STOP HELPING ICE Apr 11 '14 at 20:22
0

If the thread is created without using pthread_join then when the main thread completes execution all other threads created in main function will be stopped and hence will not complete executing the whole statements in it.

Look at the documentation of Pthread_join.

It will make the main thread to suspend until the spawned thread completes execution.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • Correct; more precisely, the invocation to `_exit()` that is in the startup code that the main thread returns to will terminate the entire process. Thus, if someone stops the main thread before it can return, the other threads will remain running, and someone else needs to terminate the process. – Simon Richter Apr 11 '14 at 16:18
  • It will block the calling thread until the target thread terminates. Does not have to be the main thread that calls join(), nor does the target thread have to have been created directly by main(). – Martin James Apr 11 '14 at 16:20
0

Well, assuming that it's an app-lifetime thread that does not need or try to explicitly terminate, the OS will do it when its process is terminated, (on all non-trivial OS).

Martin James
  • 24,453
  • 3
  • 36
  • 60