Hi,
Below is the code related to threads. The main concept is , 1 thread will
insert into list (Threadproc1) , another thread deletes from list(Threadproc3)
and third thread (Threadproc2)just prints the values in list.
I used events for sync . But I am sure this is not thread safe as some times I
observer , anoither thread is getting executed while the current one still
running. Could you please suggest how to make it thread safe.
Adv Thanks.
#include<stdio.h>
#include<conio.h>
#include<tchar.h>
#include<atlstr.h>
#include<windows.h>
typedef struct MyList
{
int i;
struct MyList *nxt;
}MyList;
int val;
HANDLE hnd_event,file_handle;
MyList *lst;
DWORD WINAPI ThreadProc1(LPVOID ptr)
{
WaitForSingleObject(hnd_event,INFINITE);
while(1)
{
ResetEvent(hnd_event);
file_handle=
CreateFile(_T("MyFile.txt"),GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
MyList *temp,*temp1;
temp = lst;
temp1 = (MyList *)malloc(1*sizeof(MyList ));
temp1->nxt = NULL;
if(lst == NULL)
{
lst = temp1;
temp = lst;
// temp->nxt=NULL;
}
else
{
temp = lst;
while(temp->nxt!=NULL)
temp = temp->nxt;
temp->nxt=temp1;
temp = temp->nxt;
}
temp->i = val;
temp->nxt = NULL;
val++;
CloseHandle(file_handle);
SetEvent(hnd_event);
}
return 1;
}
DWORD WINAPI ThreadProc2(LPVOID ptr)
{
WaitForSingleObject(hnd_event,INFINITE);
while(1)
{
ResetEvent(hnd_event);
DWORD written;
file_handle=
CreateFile(_T("MyFile.txt"),GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
MyList *temp;
CString str;
str.Empty();
temp = lst;
if(!temp)
{
// printf("\n not yet filled");
str=SysAllocString(_T("\n not yet filled.. In ThreadProc2"));
}
else
{
while(temp)
{
printf("\n The val is %d",temp->i);
str.Empty();
str.Format(_T("\n The val is %d",temp->i));
temp = temp->nxt;
}
}
WriteFile(file_handle,str.GetBuffer(),str.GetLength()*2,&written,NULL);
CloseHandle(file_handle);
SetEvent(hnd_event);
}
return 1;
}
DWORD WINAPI ThreadProc3(LPVOID ptr)
{
WaitForSingleObject(hnd_event,INFINITE);
while(1)
{
ResetEvent(hnd_event);
file_handle=
CreateFile(_T("MyFile.txt"),GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
MyList *temp,*temp1;
temp = lst;
CString str;
str.Empty();
DWORD written;
if(!temp)
{
//printf("\n empty");
str=SysAllocString(_T("\n empty"));
WriteFile(file_handle,str.GetBuffer(),str.GetLength()*2,&written,NULL);
return 1;
}
/*if(temp->nxt == NULL)
{
printf("\n Now removing %d and list is free now",temp->i);
free(temp);
temp = NULL;
return 1;
}
while(temp->nxt->nxt)
{
temp = temp->nxt;
}
free(temp->nxt);*/
while(temp->nxt)
temp = temp->nxt;
//printf("\n Now removing %d",temp->i);
str.Empty();
str.Format(_T("\n Now removing %d",temp->i));
WriteFile(file_handle,str.GetBuffer(),str.GetLength()*2,&written,NULL);
free(temp);
temp=NULL;
CloseHandle(file_handle);
SetEvent(hnd_event);
}
return 1;
}
int main()
{
hnd_event = CreateEvent(NULL,false,true,_T("GopiEvent"));
file_handle=
CreateFile(_T("MyFile.txt"),GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
CloseHandle(file_handle);
HANDLE thread_handle[3];
DWORD thid1,thid2,thid3;
thread_handle[0]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc1,NULL,CREATE_SUSPENDED,&thid1);
thread_handle[1]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc2,NULL,CREATE_SUSPENDED,&thid2);
thread_handle[2]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc3,NULL,CREATE_SUSPENDED,&thid2);
ResumeThread(thread_handle[0]);
ResumeThread(thread_handle[1]);
ResumeThread(thread_handle[2]);
WaitForMultipleObjects(3,thread_handle,true,INFINITE);
return 1;
}
Love Cricket? Check out live scores, photos, video highlights and more.
Click here http://cricket.yahoo.com
[Non-text portions of this message have been removed]