Interruptgesteuerte funktionen / Multithreading ???
-
Hi...
Ich habe volgendes problem....
ich habe 2 funktionen (rein hypotetisch) Eine Funktion Wartet auf ne eingabe, die andere gibt diese eingabe aus....
ich will jezt das es wirkt als würden beide funktionen gleichzeitig ablaufen d.h die ausgabe verändert sich durch die eingabe ...
mir wurde gesagt das ich das durch einen timerinterrupt machen kann weiss aber nicht was das ist und hab keine infos darüber gefunden....also ich will quasi das von einer funktion zur anderen im millisec bereich hinundhergesprungen wird, ohne das die funktionen wieder von vorne starten...
also quasi multitasking.... erklähr ma bidde wer wie das geht....
Swordfish schrieb mal dazu....
Musste den beitrag noch mal editieren.... irgendwie verstehe ich net wie man einen thread initialisiert und wie das hier genau funktioniert...
vlt könntest du mir ja mal n ganz simples beispiel schicken, was man auch verstehen kann.... (ich versthe ja den ganzen gotoxykrams... bzw oldx bla...)
aber ich peile nicht warum das gez funktioniert.... und nochwas hab ich mich gefragt... man kann threads synkroniesieren was iss das und wie geht das ?
bidde bidde schick mir jeman beispiele die sich für n lamer wie mich eignen und die auch n bissl erklährt sind... IHR SEID MEINE LEZTE HOFFNUNG !!!!// mit der Multithreaded Runtime linken! #include <stdlib.h> #include <stdio.h> #include <process.h> #include <time.h> #include <windows.h> int where_x( ); int get_max_x( ); int where_y( ); void gotoxy( int x, int y ); void clock( void *data ); int main( ) { if( _beginthread( clock, 0, 0 ) == -1L ) { printf( "Clock could not be started!\n" ); return 0; } char buffer[ 81 ]; printf( "Enter some Text.\n" ); printf( "Enter a single 0 (null) at the beginning of a line to exit:\n" ); do { fgets( buffer, 80, stdin ); } while( buffer[ 0 ] != '0' ); } void clock( void *data ) { time_t now_sec; tm *now_asc; int old_x, old_y; do { time( &now_sec ); now_asc = gmtime( &now_sec ); old_x = where_x( ); old_y = where_y( ); gotoxy( get_max_x( ) - 10, 0 ); printf( " %02i:%02i:%02i ", now_asc->tm_hour, now_asc->tm_min, now_asc->tm_sec ); gotoxy( old_x, old_y ); _sleep( 10 ); } while( true ); } int where_x( ) { void *output = GetStdHandle( STD_OUTPUT_HANDLE ); CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo( output, &csbi ); return csbi.dwCursorPosition.X; } int get_max_x( ) { void *output = GetStdHandle( STD_OUTPUT_HANDLE ); CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo( output, &csbi ); return csbi.srWindow.Right - csbi.srWindow.Left + 1; } int where_y( ) { void *output = GetStdHandle( STD_OUTPUT_HANDLE ); CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo( output, &csbi ); return csbi.dwCursorPosition.Y; } void gotoxy( int x, int y ) { void *output = GetStdHandle( STD_OUTPUT_HANDLE ); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition( output, pos ); }
aber was ist hierbei dafür verantwortlich das beide funktionen mehr oder weniger gleichzeitig ablaufen ?
Danke für eure hilfe im voraus..:!
-
Irgendwass mache ich mit old_x bzw old_y falsch der Cursor macht nicht das was er soll
*g*#include <stdlib.h> #include <stdio.h> #include <process.h> #include <textfarbe.h> //#include <windows.h> void Baltur( void *data ) { int i=0,old_x=0,old_y=0; while(1) { old_x = where_x(); old_y = where_y(); if(i==0) { Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 0 );printf("(-)(-) "); Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 1 );printf(" |__| "); Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 2 );printf(" ( ) __ "); Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 3 );printf(" )( - - "); Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 4 );printf(" ( )_( ( O ) )"); Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 5 );printf(" ) ( "); Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 6 );printf(" (_______________)"); gotoxy( old_x, old_y );_sleep(2000);i=1; } if(i==1) { Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 0 );printf("(-)(-)"); gotoxy( old_x, old_y );_sleep(2000);i=2; } if(i==2) { Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 0 );printf("(o)(-)"); gotoxy( old_x, old_y );_sleep(2000);i=3; } if(i==3) { Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 0 );printf("(O)(-)"); gotoxy( old_x, old_y );_sleep(2000);i=4; } if(i==4) { Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 0 );printf("(O)(o)"); gotoxy( old_x, old_y );_sleep(2000);i=5; } if(i==5) { Text_Farbe_Gelb;gotoxy( get_max_x( ) - 18, 0 );printf("(O)(O)"); gotoxy( old_x, old_y );_sleep(2000);i=0; } } } void clock( void *data ); void eingabe ( void *data) { char buffer[ 81 ]; Text_Farbe_Normal; gotoxy(0,5); printf( "Enter some Text.\n" ); gotoxy(0,6); printf( "Enter a single 0 (null) at the beginning of a line to exit:\n" ); do { Text_Farbe_Normal; fgets( buffer, 80, stdin ); } while( buffer[ 0 ] != '0' ); } int main( ) { //_beginthread( clock, 0, 0 ); _beginthread( eingabe, 0, 0 ); _beginthread( Baltur, 0, 0 ); gotoxy(0,7); system("pause"); }
-
Also, ohne jetzt genau zu schauen, was der cursor genau fabriziert, etc. würde ich mal sagen, du hast ein synchronisationsproblem, weil die threads paralel laufen und sich dadurch beim setzen / auslesen der cursorposition in die quere kommen können. Du solltest dir mal Semaphoren, Mutexe und Events anschauen.