Thread: controlling two threads

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    3

    controlling two threads

    I'm programming in C and using Visual Studio++. The question is about the following code:
    How can I stop the second thread within the first thread ?

    I don't know if this is the right way to solve the problem, my knowledge about threads is...poor.

    Thanks

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <conio.h>
    #include <time.h>
    #include <process.h>
    
    
    HANDLE hStdIn, hStdOut; /* standard input, output handles */
    
    void gotoxy (int col, int fil);
    
    // second thread 
    void crono(int *par);
    
    // first thread
    void main(void)
    {
    
        int n;
        
        int fil = 0;
        HANDLE handle;
    
        printf("\t Thread\n");
    	printf("\t_________\n");
    
        handle = (HANDLE) _beginthread( crono,0,&fil); // create thread
         
    	for(n=0;n<10;n++)
    	{
    		gotoxy(0,n+2);
    		printf("\ndoes it runs ???");
    		fflush(stdin);
    		getch();
    		/*-----------------------------------------*/
    		/* if(n==5) I WANT TO STOP THE 2ND THREAD */
    		/*-----------------------------------------*/
    	}
    	printf("\n\n\nNYAM !!!\t");
    
     }
    
    /***********************************************************************/
    
    void crono(int *par)
    {
    	time_t ti,tf;
    	int i,temps;
    
    	time(&ti);
    
    	do
    	{
    		//if(*param==5) _endthread;
    		gotoxy(10,15);
    		time(&tf);
    		temps=tf-ti;
    		printf("\r%d",temps);
    		for(i=0;i<90000;i++);
    		
    	}while(temps<10);
    
        _endthread();
    
    }
    /************************************************************/
    
    void gotoxy (int col, int fil)
    {
    	COORD coord;
    	HANDLE hStdout; /* get variable handler for screen*/
    	hStdout = GetStdHandle(STD_OUTPUT_HANDLE); /* assign screen handler*/
    
    	coord.X = col; coord.Y = fil;
    	SetConsoleCursorPosition (hStdout, coord);
    }
    
    /***********************************************************************/

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Wow, void main and fflush(stdin)
    Time to read the FAQs

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    3
    Ok, Ok... Homework done !

    Thanks !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM