11/16/2011

Thread Test in the C++ console mode.

The goal of this source code is how to send large data to the thread and how to wait until all thread finish their job.


I made 3 threads for testing.
I use struct to send the large data.
and I use the flags for waiting the time of the all thread finish.


<source code>


//////////////////////////////////////////////////////////////////////////
// Made by J.H.KIM, 2011 / feelmare@daum.net, feelmare@gmail.com        //
// blog : http://feelmare.blogspot.com                                  //
// My Lab : VISLAB(http://me.pusan.ac.kr)                               //
//////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <process.h>
#include <windows.h>

struct InputData
{
    int * data;
    int width;
    int height;
    int OriginW;
    int reserved;
    int flag;
};

void ThreadProcessing(void* pParam);


void main()
{

    //Data Allocation and Setting
    int W = 100;
    int H = 100;
    int * buffer;
    buffer =new int[W*H];
   
    for(int i=0; i<W*H; ++i)
    {
        buffer[i] = i;           
    }
   
    int sx,sy,w,h;

    //thread1, preparing Transite values
    InputData th1_input;   
    sx = 0; sy = 0; w = 80; h = 90; //(80x90) image processing
    th1_input.width = w;
    th1_input.height = h;
    th1_input.OriginW = W;
    th1_input.data = &buffer[sy*W+sx];
    th1_input.reserved = 1;
    th1_input.flag = TRUE;
    _beginthread(ThreadProcessing,0,&th1_input);

    //thread2, preparing Transite values
    InputData th2_input;   
    sx = 10; sy = 10; w = 50; h = 50; //(50x50) image processing
    th2_input.width = w;
    th2_input.height = h;
    th2_input.OriginW = W;
    th2_input.data = &buffer[sy*W+sx];
    th2_input.reserved = 2;
    th2_input.flag = TRUE;
    _beginthread(ThreadProcessing,0,&th2_input);

    //thread3, preparing Transite values
    InputData th3_input;   
    sx = 10; sy = 10; w = 20; h = 20; //(10x10) image processing
    th3_input.width = w;
    th3_input.height = h;
    th3_input.OriginW = W;
    th3_input.data = &buffer[sy*W+sx];
    th3_input.reserved = 3;
    th3_input.flag = TRUE;
    _beginthread(ThreadProcessing,0,&th3_input);


    //wait until all thread is finished.
    while( th1_input.flag || th2_input.flag || th3_input.flag)
    {
        Sleep(100);
    }

    //Data buffer release   
    delete[] buffer;


}


void ThreadProcessing(void* pParam){

    //copy struct data to variable
    int w = ((InputData*)pParam)->width;
    int h = ((InputData*)pParam)->height;
    int ow = ((InputData*)pParam)->OriginW;

    //Processing Sum(v^2.)
    int v;
    double sum=0;
    for(int j=0; j<h; ++j)
    {
        for(int i=0; i<w; ++i)
        {
            v = ((InputData*)pParam)->data[j*ow+i];
            sum += (v*v);
        }       
    }

    printf("%d thread value = %lf\n", ((InputData*)pParam)->reserved , sum);

    ((InputData*)pParam)->flag = FALSE;
}




Please leave comment if the source code has problem.
Thank you~