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.

1
  • 1
    See this question, stackoverflow.com/questions/22798068/…. The original code in the question calls pthread_create() repeatedly without performing a join. pthread_create() eventually fails with out of memory. Commented Apr 11, 2014 at 16:52

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Why would those calls fail just because a thread is not yet terminated?
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.
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.
@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.
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).

Comments

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.

2 Comments

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.
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().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.