LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 11-26-2012, 02:12 AM   #1
nontas.v
Member
 
Registered: Jan 2012
Posts: 32

Rep: Reputation: Disabled
pthread creation question


Hello everyone,
I've got an exercise in my university with creating threads.
The problem is that the book I have has very few informations about threads.

I did a search in the web about threads and understood that a thread can be detached or joinable.
I'm in Greece so studying about computer science in greek is sometimes really difficult when trying to determine what some phrases mean in the english bibliography :/

I'll try to translate a small part of the excercise:
Create 20 threads. These threads can either be "normal" (a normal thread is being executed for some time given and then exits), "eugenic" (a eugenic thread lets another thread to take its position) or "bad" (a bad thread chooses randomly on of the other threads, delete its attributes and terminates it)


I cannot understand these types of threads. I suppose the "eygenic" (REALLY BAD TRANSLATION-.-) is the joinable and the "bad" thread is a detached one?

I really need some light over here :/
Thanks for your time!
 
Old 11-26-2012, 06:00 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,535

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
IMHO, all threads are "normal"; but as you pointed out, with respect to pthreads there are joinable types and detachable types. As for the other types of threads you mention, I am not familiar with these. Perhaps the author of your book is discussing various paradigms with respect to how threads can be used?

FYI..

A joinable thread can be used to return status to the parent thread; in fact the parent thread can remain blocked (not required) while the child-thread is performing its task. Once the child-thread has terminated AND the parent thread has joined with it, will system resources used by the child-thread will be freed back to the system.

A detachable thread functions similar to a normal thread, however once it terminates, no status is returned to the parent thread that started it. A detachable thread's resources are automatically returned to the system upon termination.

By default, a joinable thread is created when calling pthread_create(). You can create a detachable thread by specifying the appropriate thread attribute.
 
Old 11-26-2012, 01:01 PM   #3
nontas.v
Member
 
Registered: Jan 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
I know the difference between joinable and detached threads.
Thanks anyway for your time
 
Old 11-26-2012, 02:08 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,388

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by nontas.v View Post
I'll try to translate a small part of the excercise:
Create 20 threads. These threads can either be "normal" (a normal thread is being executed for some time given and then exits), "eugenic" (a eugenic thread lets another thread to take its position) or "bad" (a bad thread chooses randomly on of the other threads, delete its attributes and terminates it)


I cannot understand these types of threads. I suppose the "eygenic" (REALLY BAD TRANSLATION-.-) is the joinable and the "bad" thread is a detached one?
I don't think the exercise is really talking about different types of threads here. Rather, every thread starts executing a function (eg the start_routine parameter of pthread_create()) that determines what the thread will do. So the "bad" thread will execute a function that makes it behave "badly", ie: kills other threads. But a lot of information is being lost in translation here, so I might be way off the mark.
 
Old 11-26-2012, 02:32 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171
Something else to bear in mind ...

"A thread" is not or should not be "a request." It should be a "worker." Set up the system so that a certain number of threads are created, in a "pool," and each one of them waits for a request to arrive in a queue. It then carries out the request (catching any exceptions that may be thrown), then enqueues the result on another queue.

If the request blows up, the thread might elect to terminate itself. The parent thread is watching for child-termination and respawns them.

When the system is to shut down, a "terminate" signal of some kind is issued such that the threads wake up (if they are asleep) and, upon orderly completion of anything they are now doing, they terminate themselves. They might have serviced thousands of requests during their lifetime.
 
Old 11-26-2012, 02:56 PM   #6
nontas.v
Member
 
Registered: Jan 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
Hello ntubski, I would like to thank you at first for your time
Well it looks like after reading the excercise hundreds of times I got to the same conclusion like you.
I have one more question wich might will solve my problem.
In the pthread_create() call, the second argument is the attribute of the thread. As I saw in many tutorials and other examples most of the people use the defaults arguments by passing "NULL" as the attr.
Is it easy to describe with few words what exactly the attributes of a thread can do? Cause I searched a lot about that but couldn't understand at all :/
I might ask too much, I'm sorry but I never worked with threads before, it's something tottaly new to me...

Thanks sundialsvcs for your time also For now I just want to learn about thread creation, (as you see i'm totaly starter) when I do this then I can go further

P.S. Sorry for my bad english..
 
Old 11-26-2012, 06:00 PM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,535

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by nontas.v View Post
As I saw in many tutorials and other examples most of the people use the defaults arguments by passing "NULL" as the attr.
Is it easy to describe with few words what exactly the attributes of a thread can do?
You might find some helpful information if you peruse the man-page for pthread_attr_init(). There are related functions for setting pthread attributes; for example pthread_attr_setdetachstate(), pthread_attr_setinheritstate(), etc.
Code:
$ man pthread_attr_init
 
Old 11-27-2012, 02:40 PM   #8
nontas.v
Member
 
Registered: Jan 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thank you everyone
 
Old 11-27-2012, 04:56 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171
One more thing ... "Google it!" There's a lot of source-code out there such that Google will find it. Documentation is great, but source-code is even better, especially if the programmer ... ... wrote some documentation in the source-code. Look around for some actual, working examples of what you have in mind. No matter what you're doing, you're not the first one to have done it.
 
Old 11-27-2012, 06:33 PM   #10
nontas.v
Member
 
Registered: Jan 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
Yeah I know what you mean, and of course I googled it a lot before I ask here The problem is I couldn't understand what the excercise was really about because of the bad translation. But I think I figured sth out

thanks for your time
 
Old 11-29-2012, 11:11 AM   #11
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,330

Rep: Reputation: 254Reputation: 254Reputation: 254
Also IBM offers some tutorials about threads.
 
  


Reply

Tags
pthreads


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
pthread and mutex question Michael23 Programming 2 11-16-2009 04:53 AM
warning in pthread creation nathan Programming 1 09-09-2009 07:49 AM
A question about pthread Stephen.Xin Programming 1 08-23-2006 12:26 AM
pthread Creation failing arunka Programming 1 07-18-2006 01:34 PM
pthread creation problem oulevon Programming 2 04-24-2006 05:59 AM

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

All times are GMT -5. The time now is 01:55 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration