LinuxQuestions.org
Review your favorite Linux distribution.
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 10-12-2007, 09:51 AM   #1
tianqio
LQ Newbie
 
Registered: Aug 2005
Location: china
Posts: 4

Rep: Reputation: 0
pthread sleep questions


hi,all:
I met a problem with different scopes of sleep() which is called in a pthread context, in my redhat9 system,the call to sleep() only sleeps the calling thread, while it sleeps the whole process in fc6! Does anybody can tell me the reason of this different behaviors. Does it because the different pthread lib? in redhat9, the lib is from nptl.

thanks.

A detail explanation is warmly welcomed!
 
Old 10-12-2007, 08:34 PM   #2
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
sleep() should not be used from within a thread. Different implementations will do different things. Some will allow sleep() to only sleep the thread, while others will have it block all threads in the process.

One alternate approach is to call select(), with an empty set of descriptors, and give that a timeout value. This will ensure that you sleep only 1 thread for precisely the amount of time you want.
 
Old 10-13-2007, 05:28 AM   #3
tianqio
LQ Newbie
 
Registered: Aug 2005
Location: china
Posts: 4

Original Poster
Rep: Reputation: 0
I just have a test. select() behaves the same to sleep(), when call to sleep() sleep a thread, the call to select() does the same, while sleep() sleep the whole process, so does the select() call!
Any other suggestion?
 
Old 10-14-2007, 09:24 PM   #4
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by tianqio View Post
I just have a test. select() behaves the same to sleep(), when call to sleep() sleep a thread, the call to select() does the same, while sleep() sleep the whole process, so does the select() call!
Any other suggestion?
Sounds to me like you're locking, or calling the select from the same functions, or you're not correctly testing. Post some code so we can see, as I use multi-threaded programs all the time, and my sleep routine involves calls to select, which correctly sleep a single thread, but not the entire process.
 
Old 10-14-2007, 09:40 PM   #5
tianqio
LQ Newbie
 
Registered: Aug 2005
Location: china
Posts: 4

Original Poster
Rep: Reputation: 0
excuse me, what does locking and calling from the same function mean?
here is my code:
Code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

void _sleep(unsigned int t){
        struct timeval tm;
        tm.tv_sec = t;
        tm.tv_usec = 0;

        select(0,NULL,NULL,NULL,&tm);
}
void* t1(void* a){
        pid_t t = getpid();
        printf("pid = %d\n",t);

        printf("thread %d begins\n",pthread_self());
//      sleep(10);
        _sleep(10);
        printf("thread %d ends\n",pthread_self());
}

void* t2(void* a){
        pid_t t = getpid();
        printf("pid = %d\n",t);

        int i = 0;
        printf("thread %d begins\n",pthread_self());
        for(; i < 10 ;++i){
                printf("thread %d is running\n",pthread_self());
                //sleep(1);
                _sleep(1);
        }
}
int main(){
        pthread_t tid1;
        pthread_t tid2;

        pthread_create(&tid1,NULL,t1,NULL);
        pthread_create(&tid2,NULL,t2,NULL);
        pthread_join(tid1,NULL);
        pthread_join(tid2,NULL);
}
and the result in my redhat 9 is:
pid = 2732
thread 1082399936 begins
pid = 2732
thread 1090788416 begins
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1090788416 is running
thread 1082399936 ends

It seems the call to select() only sleeps the calling thread.
but in fedoracore6 the select() call sleeps the whole process. Last friday, I found this problem in office, but now I can't access that machine,so I can't give you the test result. But I'm sure the call to select() will sleep the whole process accords to my test in last friday.

Last edited by tianqio; 10-14-2007 at 09:47 PM. Reason: add run result
 
Old 10-15-2007, 08:57 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by orgcandman View Post
sleep() should not be used from within a thread. Different implementations will do different things. Some will allow sleep() to only sleep the thread, while others will have it block all threads in the process.
I would use nanosleep since that's what it's intended for. Technically sleep should pause the process, but it's up to interpretation if that's the main thread, all threads, or the current thread.
ta0kira
 
  


Reply


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
libtest.a uses pthread: user of libtest.a should not link pthread again debulu Programming 2 01-31-2007 09:23 PM
Searching for Server (BOOTP/DHCP) <sleep> <sleep> .. .. Eileen Linux - Networking 12 10-21-2005 01:14 AM
pthread zaman Programming 6 08-29-2004 04:04 PM
pthread and C++ gt73 Programming 17 11-04-2003 03:01 AM
Pthread rch Programming 1 05-28-2003 02:20 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration