LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-05-2014, 04:52 PM   #1
brentw
LQ Newbie
 
Registered: Aug 2014
Posts: 29

Rep: Reputation: Disabled
sleeping in a thread


Hello, I will preface this with the fact I am a newbie.

I am running angstrom 3.2.0 on a AM335x kit. I have created an application with a thread that checks to see if a USB drive has been attached.

In the thread where I periodically check to see if the Usb Host has be mounted or not , I sleep.

I have a couple of questions.

1) should i use sleep(), nanosleep(), or pthread_yield()?

2) the other dumb question is i was expecting the application thread to be running in the background allowing me to continue using the command line. this is not the case, the command line is locked up while the application thread is running. is there anyway around this (i.e. i can run my application in the background and still have access to the command line?)

thanks for your help
 
Old 09-06-2014, 07:42 PM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
1) not an expert, but i'd say sleep.

2) put an "&" (without quotes) at the end of the command, then press enter.
 
Old 09-06-2014, 08:27 PM   #3
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You should NOT use sleep.

You can try:
http://www.qnx.com/developers/docs/6...epon_wait.html
http://somethingswhichidintknow.blog...n-pthread.html

What I do when using threads is never assume a function is thread safe. Try as much as possible to use pthreads functions and to make sure other functions are thread safe.

Also, I would make sure that I really need a thread before starting one.
 
Old 09-07-2014, 07:23 AM   #4
brentw
LQ Newbie
 
Registered: Aug 2014
Posts: 29

Original Poster
Rep: Reputation: Disabled
Thanks a lot this helps.

Again a dumb question. You mention to make sure that I really need a thread before starting one.

Like I mentioned, my application periodically checks to see if a USB drive is there and process accordingly. It only does this. So I only have one thread.

If I did not create a thread and just ran my code in a while loop in main. Does this just create a unique process? Are there any detrimental affects to the rest of my linux system running from doing this?

Should I use fork in the main application instead. That way I have access to the command line while mp application (process) is running?

Thanks again.

Last edited by brentw; 09-07-2014 at 08:04 AM.
 
Old 09-07-2014, 07:34 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
angstrom 3.2.0 on a AM335x kit
what is this exactly?

are you talking about shell scripting or programming, what sort of linux (distro) are you running there?
 
Old 09-07-2014, 09:53 AM   #6
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by brentw View Post
Thanks a lot this helps.

Again a dumb question. You mention to make sure that I really need a thread before starting one.

Like I mentioned, my application periodically checks to see if a USB drive is there and process accordingly. It only does this. So I only have one thread.

If I did not create a thread and just ran my code in a while loop in main. Does this just create a unique process? Are there any detrimental affects to the rest of my linux system running from doing this?

Should I use fork in the main application instead. That way I have access to the command line while mp application (process) is running?

Thanks again.
I don't think you need a thread for this. Just run it in the main loop. As long as you let the system sleep using whatever sleep function you want, there should be no detrimental effects.

You can send a program to the background using '&' as mentioned above. It will output its pid in case you need to kill it.
 
1 members found this post helpful.
Old 09-07-2014, 10:01 AM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Maybe use daemon() (man 3 daemon)
 
1 members found this post helpful.
Old 09-07-2014, 10:52 AM   #8
brentw
LQ Newbie
 
Registered: Aug 2014
Posts: 29

Original Poster
Rep: Reputation: Disabled
Thanks for the information!
 
  


Reply

Tags
embedded, linux, sleep, thread



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
python: why threading.thread pass empty dictionary to function thread golden_boy615 Programming 0 03-31-2014 11:45 AM
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
the return value of getpid() called from main thread and new thread r identical !!! cryincold Programming 3 02-29-2008 01:37 AM
Sleeping Nukem Mandriva 6 01-07-2004 05:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:26 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