Hi Vishal,
This is somewhat similar to producer consumer .. But not 100%
similar to that. I have written this code just to learn Thread safety measures
that I need to take care.
Thx,
GopiKrishna Komanduri
Software engineer
[email protected]
--- On Mon, 17/8/09, vishal thakur <[email protected]> wrote:
From: vishal thakur <[email protected]>
Subject: Re: [c-prog] Request to help regarding thread safety!
To: [email protected]
Date: Monday, 17 August, 2009, 10:24 AM
Gopi,
First I don't think that this program need to be multithreaded, as all three
operations are atomic, so if one operation is going on then you cann't
invoke another operation, else it will currupt the list.
So making this program as multithreaded will only create the extra load and
you will not gain anything out of this.
Do share your thoughts.
Regards,
Vishal
On Sun, Aug 16, 2009 at 11:54 PM, Gopi Krishna Komanduri <
gopikomanduri@ yahoo.com> wrote:
>
>
> 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]);
> WaitForMultipleObje cts(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]
>
>
>
--
Regards
Vishal Thakur
+919923206953
[Non-text portions of this message have been removed]
Yahoo! recommends that you upgrade to the new and safer Internet Explorer
8. http://downloads.yahoo.com/in/internetexplorer/
[Non-text portions of this message have been removed]