#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Events in C and problems in the program


    When I write the code:

    Code:
     #define _CRT_SECURE_NO_WARNINGS /* to suppress Visual Studio 2010 compiler warning */
    
    #include "MoreTypes.h"
    #include <stdio.h>
    #include <Windows.h>
    #include "Bmp24LineReader.h"
    #include "Dynamic2DCharArray.h"
    #include "Hw3Types.h"
    #include "IspImage.h"
    #include "IspImageAnalysis.h"
    #include <string.h>
    	
    /**
     * LoadPicture() change picture from colors to gray.
     * 
     * accepts:
     * -------
     * Picture - the color picture.
     * Pitch - the size of line in bytes
     * Height - the height of the picture in pixeles
     * Width - the width of the picture in pixles
     *
     * Returns:
     * -------
     * This function doesn't return anything - it change a global variables:
     * PicGrayArr - the gray picture
     * ArrOfEvents - array that signals everytime row has been changed to gray
     */ 
    
    void LoadPicture(ScandAPictureRowStruct* ThreadData) 
    {
    	EReadRowRes res;
    	Byte_t* LineBuffer;
    	Byte_t Red=0, Green=0, Blue=0, Gray=0;
    	int j, RowNum=0;
    	char num=0;
    	DWORD dwWaitResult=0, ReleaseRes; 
    	int Pitch=0, Width=0, Height=0;
    	
    	printf("Enter Thread Function\n");
    
    
    	Height = ThreadData->Height;
    	Width = ThreadData->Width;
    	Pitch= ThreadData->Pitch;
    
    
    	//creating space for the line
    	LineBuffer = (Byte_t*) malloc (sizeof(Byte_t) * Pitch);
    	if (LineBuffer==NULL)
    	{
    		//handle error
    		printf("Memory Allocation Failed\n");
    		//handle error
    	}
    	//reading the picture and making in gray
    	for (RowNum=0; RowNum < Height; RowNum++)
    	{
    		//reading color line
    		res = ReadRow (picture, LineBuffer);
    		if (res==FAILURE)
    		{
    			CloseBmp24LineReader( &picture );
    			//handle error
    		}
    		if (res != END_OF_FILE)
    		{
    			//convert pixel from color to gray
    			for (j=0; j<Width; j=j+3)
    			{
    				Blue = *(LineBuffer+j);
    				Green = *(LineBuffer+j+1);
    				Red = *(LineBuffer+j+2);
    				num=ConvertPixelColorToGrayscale(Blue, Green, Red);  	 // Convert Color Pixel to Gray Pixel
    				PicGrayArr[RowNum][j]=num;
    			}
    		//signal that the line was loaded 
    		SetEvent(ArrOfEvents[RowNum]);
    		}
    	}
    	ReleaseRes = ReleaseSemaphore(semap, 1, /* Signal that exactly one cell was emptied */NULL );	/* We don't care what the previous count was */
    	if ( ReleaseRes == FALSE ) 
    	{
    		//handle error
    	}
    }
    
    
    void main(int argc, char *argv[])
    
    {
    	// ******* Variables *********////////
    	int Pitch=0, i=0, j=0;
    	char num=0; 
    	char* PicFileName=NULL, *OutputFileName=NULL, *str=NULL;
    	int Height=0, Width=0;
    	int FaceWidth=0, FaceHeight =0, NumOfAreasVertical = 0, NumOfAreasHorizontal = 0, NumOfMaxThreads;
    	DWORD threadID, WaitRes;
    	Byte_t *LineBuffer=NULL,  **LineBufferPointerArr=NULL;
    	ScandAPictureRowStruct ThreadData;
    	HANDLE ReadPicThread;
    //	// ***** Variables ********///////
    //
    //
    ////	printf("hello\n"); 
    //
    //	// ****** Input Handling  ***********// 
    //	if (argc!=8)
    //	{
    //		printf ("Input Error\n");
    //		//handle error
    //	}
    
    	PicFileName = argv[1];
    	OutputFileName = argv[2];
    	FaceWidth = atoi(argv [3]);
    	FaceHeight = atoi(argv[4]);
    	NumOfAreasVertical = atoi(argv[5]); 
    	NumOfAreasHorizontal = atoi(argv [6]);
    	NumOfMaxThreads = atoi(argv [7]); 
    //
    //	// ****** Input Handling ************//
    //
    	//create semaphore
    	semap = CreateSemaphore( NULL, // default sec. attr.
    	NumOfMaxThreads, // initial count
    	NumOfMaxThreads, // max count
    	NULL ); // no name
    
    //	
    //	//create events. We create an event for each row. Once the event is signaled it means that we have already loaded the current row
    	ArrOfEvents = (HANDLE*) malloc ( sizeof(HANDLE) * Height); 
    	if (ArrOfEvents == NULL)
    	{
    		printf("error!\n");
    		//handle error
    	}
    
    	//read the picture
    	picture= OpenBmp24LineReader(PicFileName);
    	if ( !IsInitialized( picture ) ) 
    	{
    		printf("error!\n");
    	//	handle error
    	}
    	Pitch = GetPitch(picture); // Get number of bytes in a row
    	Width = GetWidth (picture);
    	Height = GetHeight (picture);
    
    	PicGrayArr = CreateDynamic2DCharArr (Height, Width);
    
    	
    	//initial event array
    	for(i=0; i < Height; i++)
    	{
    		ArrOfEvents[i] = CreateEvent(NULL, TRUE /* manual reset*/, FALSE /*initial state is not signaled*/ , NULL /* Object not named*/ ); 
    		if (ArrOfEvents[i]==NULL)
    		{
    			printf("Create Event Error\n"); 
    		//	handle error
    		}
    	}
    
    	////creating thread to read the picture
    	ThreadData.picture = picture;
    	ThreadData.Pitch = Pitch;
    	ThreadData.Height = Height;
    	ThreadData.Width = Width;
    
    	
    	printf("Before Sema\n");
    	printf("Before Thread\n");
    
    	ReadPicThread=CreateThread( NULL, 0 /*use default stack size*/, (LPTHREAD_START_ROUTINE) LoadPicture /* thread function*/,
    	&ThreadData /* argument to thread function */,0 ,&threadID	/* returns the thread identifier */  );
    
    
    	WaitRes = WaitForSingleObject(ReadPicThread, INFINITE );
    	if ( WaitRes != WAIT_OBJECT_0 ) 
    	{
    		//hanle error
    	}
    
    
    	CloseBmp24LineReader( &picture );
    	free (LineBufferPointerArr);
    	FreeDynamic2DCharArr(PicGrayArr);
    }
    I find out that when the program reaches the line of:

    printf("Before Sema\n");
    it's stuck, and exits without printing the line.

    Howeven, once I omit the lines:

    Code:
    for(i=0; i < Height; i++)
    {
    ArrOfEvents[i] = CreateEvent(NULL, TRUE /* manual reset*/, FALSE /*initial state is not signaled*/ , NULL /* Object not named*/ ); 
    if (ArrOfEvents[i]==NULL)
    {
    printf("Create Event Error\n"); 
    //
    handle error
    }
    }
    The problem disappears, meaning the program doesn't get stuck at all and continues to run.

    What may be the problem with the Create Events?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Unlikely to be the problem but easy to verify, what's the value of Height in that loop?
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo