is there a way to run 2 functions at once?
i heard it was called threading. :S
thanks for the help.
is there a way to run 2 functions at once?
i heard it was called threading. :S
thanks for the help.
Sure can. How to do it depends on your platform though.
basically i want to write an alarm program where the user has 10 seconds to enter the correct pin.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%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"); } }
Here was where I learned. The learning curve isn't that steep.
http://msdn.microsoft.com/en-us/libr...47(VS.85).aspx
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.
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("(%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; }
Mainframe assembler programmer by trade. C coder when I can.
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.
We all know what you want to do, but we aren't going to do it for you.
What don't you get?
Mainframe assembler programmer by trade. C coder when I can.
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.
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.