pthread - How to start running a new thread without calling join?
Solution 1
The function to start the thread is pthread_create
, not
pthread_join
. You only use pthread_join
when you are ready to wait,
and resynchronize, and if you detach the thread, there's no need to use
it at all. You can also join from a different thread.
Before exiting (either by calling exit
or by returning from main
),
you have to ensure that no other thread is running. One way (but not
the only) to do this is by joining with all of the threads you've
created.
Solution 2
the behaviour of your code depends on the scheduler; probably the main program exits before printf in the created thread has been executed. I hope simple sleep(some_seconds) at the end of the main() will cause the thread output to appear :)
Solution 3
the join
call waits for the thread to terminate and exit.
if you want your main thread to continue its execution while the child thread is executing, don't call join
: the child thread will execute concurrently with the main thread...
Solution 4
Just create the thread with the detached attribute set to on. To achieve this, you can either call pthread_detach
after the thread has been created or pthread_attr_setdetachstate
prior to its creation.
When a thread is detached, the parent thread does not have to wait for it and cannot fetch its return value.
Solution 5
you need to call pthread_exit in the end of man(), which will cause main to wait other thread to start and exit. Or you can explicitly call pthread_join to wait the newly created thread
Otherwise, when main returns, the process is killed and all thread it create will be killed.
Ohad Horesh
Updated on May 10, 2020Comments
-
Ohad Horesh almost 4 years
I want to start a new thread from the main thread. I can't use join since I don't want to wait for the thread to exit and than resume execution.
Basically what I need is something like pthread_start(...), can't find it though.Edit:
As all of the answers suggested create_thread should start thread the problem is that in the simple code below it doesn't work. The output of the program below is "main thread". It seems like the sub thread never executed. Any idea where I'm wrong?
compiled and run on Fedora 14 GCC version 4.5.1void *thread_proc(void* x) { printf ("sub thread.\n"); pthread_exit(NULL); } int main() { pthread_t t1; int res = pthread_create(&t1, NULL, thread_proc, NULL); if (res) { printf ("error %d\n", res); } printf("main thread\n"); return 0; }
-
Mel almost 13 yearsAlso, it doesn't warrant a full answer, but if you don't join the threads you created, your program will exit before they done anything as execution in the main thread will go on.
-
Klas Lindbäck almost 13 years+1 Actually, you don't have to wait for the other threads before exiting. Calling exit() will automatically terminate all threads. If you want to terminate gracefully you may want to use pthread_barrier or pthread_join, though.
-
James Kanze almost 13 years@Klas Lindback Calling
exit()
will terminate the entire process, which of course terminates all threads. But this isn't an instantaneous process; there is a long list of steps thatexit
does, and terminating the threads is near the end of them. In the meantime, destructors of static variables have been called, memory structures (including those ofmalloc
?) cleaned up, etc., etc. In the meantime, other threads continue to run, eventually using resources which have been cleaned up. C++11 also adds restrictions. -
Ohad Horesh almost 13 yearsThanks @user396672 indeed this was the problem. I choose James answer since it matched the original question but at least I gave you an upvote :)
-
Eduard Luca about 11 yearsOoooooh, I see now. Was wondering why my threads didn't get executed if I called
pthread_detach
... I understand how it works now. This is actually good for daemons / services, who don't ever exit (at least in theory). Thanks! -
user1284631 over 9 yearsThis is the correct answer. The correct solution to wait for the spawned thread to run before returning from main() is to call pthread_join(t1, NULL) before returning. pthread_create() provides no guarantee that the thread starts running as soon as it returns. More, the time until the thread is actually scheduled is indefinite. Either you signal with a condition variable that the spawned thread completed its job, either you wait.
-
Adrien Plisson about 7 yearsyes it is. what makes you think not calling join() is not standard ? (to be honest, you should still call join() at the end of the main thread to ensure that all child thread terminate before exiting the application)
-
Samer about 7 yearsDo you think that I can just simply call detach() at the of the destructor of the class?
-
Adrien Plisson about 7 yearsit depends on the design. you can do it, but it will let the thread run on its own: this is a kind of resource leak that i do not like. (but we are drifting away from the original question)