LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2019, 10:52 AM   #1
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Rep: Reputation: 13
Threads, Joinable vs Detached


My searches return references that describe how to implement joinable and detached thread, but not why. Please explain or tell me where to find an explanation that contains the concepts:

If you want to <do this> then leave the thread joinable.
If you want to <do that> then detach the thread.
 
Old 05-24-2019, 02:27 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
It is all about the future of the thread, and whether and how you need to interact with it.

If you need to get an exit status when the thread completes, or if you need to block one thread until another completes, or wish to share priority, they must be joinable.

If you don't need to hear from the thread again and trust it to complete its mission and clean up after itself, then detach it.
 
Old 05-24-2019, 02:37 PM   #3
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
When a thread is started it can be given the address of, for example, a structure. (That is the limit of my knowledge and experience so far.) It can write to that structure providing communications. I am using that to pass values back to the originator. One item of the structure tells the thread to quit and another holds a status that the thread updates. Plus some variables that the thread updates.

Does that change the concepts any?

Edit: If we need to start a thread that monitors or controls something, and it can use, maybe SOAP to communicate, and does not need the parent to do anything, then it can or should be detached.
Is that correct? Does it need clarifications?

Last edited by bkelly; 05-24-2019 at 02:41 PM. Reason: more info
 
Old 05-30-2019, 05:33 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
or use this

https://www.openmp.org/
 
1 members found this post helpful.
Old 05-31-2019, 07:58 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by bkelly View Post
When a thread is started it can be given the address of, for example, a structure. (That is the limit of my knowledge and experience so far.) It can write to that structure providing communications. I am using that to pass values back to the originator. One item of the structure tells the thread to quit and another holds a status that the thread updates. Plus some variables that the thread updates.

Does that change the concepts any?

Edit: If we need to start a thread that monitors or controls something, and it can use, maybe SOAP to communicate, and does not need the parent to do anything, then it can or should be detached.
Is that correct? Does it need clarifications?
It doesn't change the concepts, but it is really kind of orthogonal to the question of whether a given thread's state should be joinable or detached.

As with all things in programming you should first model what you want to do, then make choices of language features and methods which best fit the model, usually also adjusting the model to make best use of those choices. Then decisions such as whether to detach or make a thread joinable fall out of the model instead of being arbitrary and isolated.

As bigearsbilly has suggested, you might want to build on an existing model, or specification, such as openmp which will define an overall framework for managing threads to best advantage.

Good luck!
 
Old 06-07-2019, 01:58 PM   #6
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by bigearsbilly View Post
I was unable to discover anything relevant from either of the two links. Did I miss something?

In general I have not recognized the answer. I'll reword:

Detach a thread if you need to <do this>. Otherwise do not detach it.

Last edited by bkelly; 06-07-2019 at 02:00 PM.
 
Old 06-07-2019, 04:14 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by bkelly View Post
I was unable to discover anything relevant from either of the two links. Did I miss something?

In general I have not recognized the answer. I'll reword:

Detach a thread if you need to <do this>. Otherwise do not detach it.
OK, let's try saying it another way...

This will appear to be a tautology, but please consider it seriously...


Quote:
Detach a thread if you [ do not ] need to < join it >. Otherwise do not detach it.
Which implicitly includes the question...

Code:
When or why might I need to join a thread?
And again, the short answer to that is: When you need to get the exit status of a thread, or when you need to block one thread until another completes, or if you want to inherit or share scheduling priority among threads, then you will need to join them.

Which seems to not be the answer you are looking for, I suspect because you are really asking, "When I write some arbitrary threaded application, how can I decide when to make use of the joinable or deteched state of threads?".

And the answer to that takes us back to the model of your application - how your model makes use of threads during execution. In other words, the answer depends on where the thread fits into the application's execution model.

Where OpenMP* fits into this picture is explained in their About Us page...

Quote:
... the OpenMP API is a portable, scalable model that gives shared-memory parallel programmers a simple and flexible interface for developing parallel applications...
Similarly, if you were to ask, "When should I use an integer type vs a float type for a variable in my program?", the answer is of course, "It depends on what you intend your program to do with it after you create it!".

I also found this StackOverflow page which includes another brief discussion and may be helpful.

Hope this helps!

*Note: I am not really familiar with OpenMP and not particularly recommending it, but it is one model for programming threaded applications already mentioned and as such is an example at hand - there are many more. The point being made is the ultimate dependence of the answer upon the chosen model.

Last edited by astrogeek; 06-07-2019 at 04:35 PM. Reason: punctuation gone awry... and typos
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leak when using detached threads incoming429 Programming 1 07-14-2006 09:55 AM
my process needs a thread counter for detached threads Thinking Programming 6 01-25-2006 03:32 AM
No detached Bitchx process bugsbunny Slackware 0 04-18-2004 05:30 AM
Floppy Detached HwzrHlslndr Linux - Hardware 13 12-31-2002 03:17 PM
modem is detached??? Mk2vdub Linux - Newbie 0 12-03-2002 12:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration