473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Can someone tell the value of "pthread_exit(NULL);" in main

I was writing a small program to create a thread but i noticed that output is not coming if remove "pthread_exit(NULL);" statement in main,but if i replace it output is coming.Can someone explain the reason.


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<pthread.h>
  3. void *thread(void *t)
  4. {
  5. int i=10;
  6. while(i)
  7. {
  8. printf("%d",i);
  9. i--;
  10. }
  11. pthread_exit(NULL);
  12. }
  13. int main()
  14. {
  15. int rc;
  16. pthread_t t;
  17. rc=pthread_create(&t,NULL,thread,NULL);
  18. if(rc)
  19. {
  20. printf("error:%d",rc);
  21. return(-1);
  22. }
  23. pthread_exit(NULL);
  24. }
  25.  
Aug 21 '15 #1

✓ answered by kiseitai2

To allow other threads to continue execution, the main thread should
terminate by calling pthread_exit() rather than exit(3).

http://man7.org/linux/man-pages/man3...ad_exit.3.html

Based on various documentations and some stackoverflow comments, it seems that pthread_exit terminates main() but does not allow the process to terminate. This way, the other threads are allowed to finish execution without resources becoming invalid or without being abruptly terminated when the process terminates. Here's a schematic representation of parallel programming. For these examples I will be using Hello World as the program in execution. Forward arrows represent x spawns or executes y and backwards arrows represent a return from.

The standard single thread case (when you compile a Hello World program and run it):

hello.exe -> main_thread -> main()
This is how your typical program executes, so execution stops in reverse.
hello.exe <- main_thread <- main()

The multithreading case:

hello.exe -> main_thread -> main() ->t1
->t2
Which becomes
hello.exe -> main_thread -> main()
-> t1_thread -> t1()
-> t2_thread -> t2()
If main exits before other threads
hello.exe <- main_thread <- main()
X-> t1_thread -> t1()//X = cancelled
X-> t2_thread -> t2()//X = cancelled
If pthread_exit is called in main
hello.exe <- main_thread <- main()
-> t1_thread -> t1()
-> t2_thread -> t2()
//hello.exe waits until the last thread exits before exiting

How exactly pthread_exit() accomplishes this? I am not sure, but it definitely creates the same effect as if main_thread was another thread. It would be cool if someone who is familiar with the inner workings of pthread could clarify if main_thread is being blobked until the last child thread stops execution or if it is able to change the execution of hello at the core? I mean, if the latter is possible, how could you change the meaning of main_thread from main to !main? Anyways, the reason you weren't getting any output is because the other threads were cancelled before they could start/continue executing! I hope this explanation helps!

1 6097
To allow other threads to continue execution, the main thread should
terminate by calling pthread_exit() rather than exit(3).

http://man7.org/linux/man-pages/man3...ad_exit.3.html

Based on various documentations and some stackoverflow comments, it seems that pthread_exit terminates main() but does not allow the process to terminate. This way, the other threads are allowed to finish execution without resources becoming invalid or without being abruptly terminated when the process terminates. Here's a schematic representation of parallel programming. For these examples I will be using Hello World as the program in execution. Forward arrows represent x spawns or executes y and backwards arrows represent a return from.

The standard single thread case (when you compile a Hello World program and run it):

hello.exe -> main_thread -> main()
This is how your typical program executes, so execution stops in reverse.
hello.exe <- main_thread <- main()

The multithreading case:

hello.exe -> main_thread -> main() ->t1
->t2
Which becomes
hello.exe -> main_thread -> main()
-> t1_thread -> t1()
-> t2_thread -> t2()
If main exits before other threads
hello.exe <- main_thread <- main()
X-> t1_thread -> t1()//X = cancelled
X-> t2_thread -> t2()//X = cancelled
If pthread_exit is called in main
hello.exe <- main_thread <- main()
-> t1_thread -> t1()
-> t2_thread -> t2()
//hello.exe waits until the last thread exits before exiting

How exactly pthread_exit() accomplishes this? I am not sure, but it definitely creates the same effect as if main_thread was another thread. It would be cool if someone who is familiar with the inner workings of pthread could clarify if main_thread is being blobked until the last child thread stops execution or if it is able to change the execution of hello at the core? I mean, if the latter is possible, how could you change the meaning of main_thread from main to !main? Anyways, the reason you weren't getting any output is because the other threads were cancelled before they could start/continue executing! I hope this explanation helps!
Aug 26 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: MLH | last post by:
A97 procedure to open http://www.arch.com/message/ enter an 800 number, press "Continue", enter a text msg string from a memo field and press "Send" This is a calendaring and appointment A97...
2
by: Just Me | last post by:
Dim iData As IDataObject = Clipboard.GetDataObject() Dim img As System.Drawing.Bitmap img = img.FromFile(ofd.FileName, True) Clipboard.SetDataObject(img) TextControl1.Paste() img.Dispose() : img...
25
by: Christian Christmann | last post by:
Hi, the ANSI-C 99 standard specifies that the main function has "int" as return type. However, there are still lots of people declaring "void" as main return type. Did previous ANSI-C...
6
by: rlueneberg | last post by:
Can someone tell if Sitemap always require a starting root? Can it have more than one root? I am having a problem trying to hide a root node without hiding its children using a Treeview component....
2
sanjay123456
by: sanjay123456 | last post by:
Dear Friend , in c the return value for main() is optional why in c++ the main() must return value ? sanjay
2
by: ppuniversal | last post by:
Hello, Can someone tell a good Tutorial on Network Programming in C++, except Beej's Guide and Vijay Mukhi's tutorials. I want to make a Client Server application in C++ which will involve...
11
by: polas | last post by:
Good afternoon all, I appreciate that the standard requires that main should return an integer. What I was wondering though is there any accepted standardisation on exactly what value to return...
0
by: Saboor Mohideen | last post by:
Can someone tell me which event occurs on Total TextBox: Main Form Name Type Event Used Event Set All three textboxes are bound to a table CashAmount TextBox ...
3
by: jinnejeevansai | last post by:
Can someone tell why is variable a not getting printed. import java.lang.*; import java.io.*; class t { int a=5; void fun() { System.out.println(a);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.