To suspend a thread

This is a discussion on To suspend a thread within the Linux Programming forums, part of the Platform Specific Boards category; Is there any function to suspend a thread just like sleep() function. sleep(k) function is invoked inside a process to ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Kolkata, India
    Posts
    3

    To suspend a thread

    Is there any function to suspend a thread just like sleep() function. sleep(k) function is invoked inside a process to suspend the current process for 'k' seconds. I want a similar function but for thread level which can be invoked from the thread itself.

    Code:
    #include <pthread.h>
    #include <stdio.h>
    
    void  func(void) {
    	while (1) {  
    		int data = rand()%100;
    		printf("Current data is %d ...\n",data);
    		
    		/* here the thread should be suspended for 3 sec */
    	}
    }
    
    int main(void) {
        pthread_t thread1;
    
        srand(time(NULL));
    
        printf("\n");
    
        /* Spawn the Thread */
        pthread_create( &thread1, NULL, func, NULL );
    
        /* to join the thread */
        pthread_join( thread1, NULL );
    
        printf("\n");
    
        return 0;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    sleep() does work on a thread-level basis, just as you want. If one thread is sleeping then another can continue execution.

    What are you trying to do, exactly?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Kolkata, India
    Posts
    3
    Actually, I was implementing a reader's writer's problem, where each reader and writer will be a separate thread. I want the threads to be suspended for a random time. I have tried the sleep(). But it is resulting problems. Threads are supposed to print after reading or writing and this print operation is kept in critical section and hence protected by the semaphore (see the code below). But this printing operation is not executed in proper order.

    Code:
    void *READER(void *arg)
    {
    	while(1) {
    		Lamport_Bakery_read_count(0);	/* waiting for 'read_count' */
    		
    		read_count++;
    		if(read_count == 1)
    			Lamport_Bakery_write_access(0);	/* waiting for 'write_access' */
    
    		Exit_for_read_count(0);	/* signal on 'read_count' */
    
    		/* READ OPERATION */
    		printf("Reader 1 read %u ... \n",data);
    		fflush(stdout);
    		        
    		Lamport_Bakery_read_count(0);	/* waiting for 'read_count' */
    
    		read_count--;
    		if(read_count == 0)
    			Exit_for_write_access(0);	/* signal on 'write_access' */
    
    		Exit_for_read_count(0);	/* signal on 'read_count' */
    		
    		/* threads is to be suspended here */
    	}
    }
    
    void *WRITER(void *arg)
    {
    	while (1) {  
    		Lamport_Bakery_write_access(3);	/* waiting for 'write_access' */
    		
    		/* WRITE OPERATION */
    		data = rand()%100;
    		printf("Writer 3 wrote %d ...\n",data);
    		fflush(stdout);
    		//sleep(3);
    		
    		Exit_for_write_access(3);	/* signal on 'write_access' */
    		
    		usleep(rand()%3000 + 500);
    	}
    }

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    Assuming that Lamport_Bakery_XXX and Exit_for_XXX are implemented using proper synchronization (via pthread_mutex_t etc.), the biggest issue I see is that multiple READER's can starve all WRITER's due to read_count never reaching 0.

    Lamport's bakery algorithm, Dekker's algorithm, Peterson's algorithm, etc - None of these can be implemented in pure C. The assumptions these algorithms make do not hold under modern compilers and hardware.

    So if Lamport_Bakery_XXX and Exit_for_XXX don't seem to be providing mutual exclusion, that's probably because they aren't.

    gg

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Kolkata, India
    Posts
    3
    Thanks to you all.
    I have found the solution. sleep() may be implemented at process level. But for thread level blocking, it may be better to use pthread_cond_timedwait(). Please see this or this for detailed information.

    Thank you again for helping.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    >> sleep() may be implemented at process level.
    Wrong.

    http://www.opengroup.org/onlinepubs/...ons/sleep.html

    If the system if Posix compliant, then it's at the thread level.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-25-2010, 07:14 AM
  2. Listening socket and thread problem
    By esaptonor in forum Windows Programming
    Replies: 6
    Last Post: 06-19-2010, 03:04 AM
  3. Replies: 2
    Last Post: 02-26-2009, 10:48 PM
  4. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 04:14 AM
  5. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21