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);
}
/***********************************************************************/