Threading?

This is a discussion on Threading? within the C Programming forums, part of the General Programming Boards category; is there a way to run 2 functions at once? i heard it was called threading. :S thanks for the ...

  1. #1
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24

    Threading?

    is there a way to run 2 functions at once?

    i heard it was called threading. :S

    thanks for the help.

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Sure can. How to do it depends on your platform though.

  3. #3
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    Code:
    # include <stdio.h>
    # include <conio.h>
    # include <stdlib.h>
    # include <windows.h>
    
    void countdown(int);
    void sec_code (int);
    
    void main (int i)
    {
    	countdown(i);
       sec_code(i);
       printf ("\t\tAlarm Diabled");
       getch();
    }
    
    void countdown(int i)
    {
    
    	i =10;
    
       while( i >0)
       {
          if (i ==10)
          {
          	gotoxy(10,10);
          }
          else
          {
          	gotoxy(11,10);
          }
       	printf ("\t\t&#37;d seconds remaining",i);
          i--;
          Sleep(1000);
    
          if (i == 0)
          {
             printf ("\n\n\t\t\a\aALARM!");
          }
       }
    }
    void sec_code (int i)
    {
    	char code[10];
       printf ("\n\n\t\tEnter code: ");
       scanf("%s",code);
    
       if (strcmp(code,"12345")==0 && i > 0)
       {
       	printf ("\t\tAlarm Disarmed");
       }
    }
    basically i want to write an alarm program where the user has 10 seconds to enter the correct pin.

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Here was where I learned. The learning curve isn't that steep.
    http://msdn.microsoft.com/en-us/libr...47(VS.85).aspx

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    32
    For something so straightforward I wouldn't use threads at all. I would have the thing in a loop waiting up to 1000ms each time to see if a character is ready to be read. If so, read it. Either way display your countdown timer there.

    I don't normally write Win32 console apps that need to check for a character so I don't have the code in front of me; it is relatively trivial though.

    Now if you _want_ to learn to use threads just because (and I would recommend it), then that is quite another matter.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Here's a simple ALARM program that kicks off a thread, puts it to sleep for so many seconds, and then prints a msg and goes away. (From "Programming with POSIX Threads")

    Code:
    #include <pthread.h> 
    #include "errors.h"
    
    typedef  struct  alarm_tag { 
    	int seconds ; 
    	char message[64] ; 
    } alarm_t ;  
    
    
    void * alarm_thread(void * arg) { 
    	alarm_t * alarm = ( alarm_t *) arg ; 
    	int status ; 
    	
    	status = pthread_detach( pthread_self() ); 
    	if (status != 0 ) 
    		err_abort (status, "Detach Thread") ; 
    	sleep(alarm->seconds) ; 
    	printf("(&#37;d) %s\n", alarm->seconds, alarm->message) ; 
    	free(alarm) ; 
    	return NULL ; 
    } 
    
    
    int main (int argc, const char * argv[]) {
    
    	char line[128] ; 
    	alarm_t * alarm ; 
    	pthread_t thread ; 
    	
    	int status ; 
    		
    	while(1) { 
    		printf("Alarm> ") ; 
    		if (fgets(line, sizeof(line), stdin) == NULL) exit(0) ; 
    		if (strlen(line) <= 1 ) continue ; 
    		alarm = malloc(sizeof(alarm_t)) ; 
    		if (alarm==NULL) 
    			errno_abort("Allocate Alarm") ; 
    		
    		if (sscanf( line, "%d %64[^\n]", &alarm->seconds, alarm->message) < 2) { 
    			fprintf(stderr, "Bad Command\n") ; 
    			free(alarm) ;  
    		}
    		else { 
    			status = pthread_create ( &thread, NULL, alarm_thread, alarm) ; 
    			if (status != 0) 
    				err_abort(status, "Create Alarm Thread") ; 
    		}
    	}
    	return 0;
    }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    i'm still stump.

    i want a count down timer to run the background while the user gets a chance to enter correct pass which will "deactivate" in a sense, the timer.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    We all know what you want to do, but we aren't going to do it for you.

    What don't you get?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Plus the ALARM program posted does almost what you want. You just need to make the "read pin" part and connect them (which isn't hard).

    Make the "read pint" part and if you have further difficulties post again.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    no threads are needed here, as core_cpp said it earlier, there a limited cases in which threads are useful, particularly on a uniprocessor. For the problem stated, a simple "wait for events" would do the trick like: [p]select() which blocks until data is available in a stream or on timeout (it can do much more, but nevermind). I'm not sure but it should be available (or any equivalent) on Windows(c) (surely someone knowns here). It is much more simpler and efficient than using threads.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ threading
    By Anddos in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2005, 03:29 PM
  2. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 01:16 PM
  3. Problem with threading
    By osal in forum Windows Programming
    Replies: 6
    Last Post: 07-21-2004, 01:41 PM
  4. starting to learn multi threading
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2004, 02:44 PM
  5. Threading
    By threads in forum C# Programming
    Replies: 0
    Last Post: 01-17-2003, 11:50 PM

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