te mando mi código... //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool UseMutex;
HANDLE hMutex;
HANDLE hSemaforo;
int GlobalData;
Mihilo *T1;
Mihilo *T2;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form1->Caption=20;
//hMutex = CreateMutex(0,false,0);
int inicio_semaforo=atoi(Form1->Edit3->Text.c_str());
hSemaforo = CreateSemaphore(NULL,inicio_semaforo,1,0);
//UseMutex=false;
T1 = new Mihilo(false,2);
// T2 = new Mihilo(false,1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
CloseHandle(hMutex);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
delete T1;
delete T2;
}
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include "Unit2.h"
#include "Unit1.h"
#include <winbase.h>
#pragma package(smart_init)
long previo=0;
//extern bool UseMutex;
extern HANDLE hSemaforo;
void Traza(AnsiString s,bool afichero);
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall Unit2::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall Mihilo::Mihilo(bool CreateSuspended, int x)
: TThread(CreateSuspended)
{
campo=x;
contador=10000;
}
//---------------------------------------------------------------------------
void __fastcall Mihilo::RunUpdate()
{
char * cadena=(char *)malloc(20);
int x=atoi(Form1->Edit1->Text.c_str());
if(campo==1)
{
x++;
}
else if(campo==2)
{
x--;
}
Form1->Edit1->Text=itoa(x,cadena,10);
free(cadena);
}
//---------------------------------------------------------------------------
void __fastcall Mihilo::Execute()
{
bool z=true;
for(;
{
Traza("asdsdf",true);
WaitForSingleObject(hSemaforo,INFINITE);
if(campo==1)
{
//if(Form1->Caption<20)
{Synchronize(RunUpdate);}
}else if(campo==2)
{
//if(Form1->Caption>1)
{Synchronize(RunUpdate);}
}
z=ReleaseSemaphore(hSemaforo,1,&previo);
}
}
//---------------------------------------------------------------------------
void Traza(AnsiString s,bool afichero)
{
long previo=0;
AnsiString linea=(TDateTime::CurrentDateTime()).DateTimeString()+" : "+s+"\n";
FILE *f;
//zona crítica, intentar salir lo antes posible de ella.
if (afichero)
{
WaitForSingleObject(hSemaforo,INFINITE);
f=fopen("d:\\traza.txt", "a");
fprintf(f,linea.c_str());
fclose(f);
ReleaseSemaphore(hSemaforo,1,&previo);
}
//mostrar form con error.
TForm *mensaje=new TForm(Application);
mensaje->Caption=linea;
mensaje->Show();
return;
}