CreateThread in vc++

i'm a beginner can somebody help me with CreateThread:

LPTHREAD_START_ROUTINE lpStartAddress,  
LPVOID lpParameter.

also help for CreateProc.        
kaushik787Asked:
Who is Participating?
 
havman56Connect With a Mentor Commented:

see this code and use it!

u should set projectsettings->c/c++->code generation->multithreaded


void CreateInputThread();
DWORD WINAPI InputFunc( LPVOID lpParam ) ;


void CreateInputThread()
{

     DWORD dwThreadId, dwThrdParam = 1;
    char szMsg[80];

hInputThread = CreateThread(
        NULL,                        // no security attributes
        0,                           // use default stack size  
        InputFunc,                  // thread function
        &dwThrdParam,                // argument to thread function
        0,                           // use default creation flags
        &dwThreadId);                // returns the thread identifier
 
     if (hInputThread == NULL)
   {
      printf("thread creation failed");
   }
   else
   {
     // _getch();
     // CloseHandle( hInputThread );
   }

}


 DWORD WINAPI InputFunc( LPVOID lpParam )
{
     

      while(1)
      {
           InputStartOperation();
         
      }
 
 
   return 0;
}
0
 
eranborovikCommented:
It is not recommended to use CreateThread as it causes
memory leaks from c/c++ run-time libraries.
The correct call should be _beginthread.

To use it you must include <process.h>

Its usage is quite straightforward:
 #include <process.h>

unsigned long _beginthread( void( __cdecl *start_address )( void * ), unsigned stack_size, void *arglist );

start_address is a pointer to function that receives a pointer to void as a parameter. This function will start executing if the thread was successfully created. Be aware that it is possible for it to start running even before _beginthread returns!! You can set stack_size to 0
so the system will use the default. Arglist is the variable
your thread function receives.
You can also use _beginthreadex to allow for additional parameter but _beginthread is good enough for all general usages.
The _beginthread function returns -1 on error, to further query the error inspect errno.
The thread terminates when your thread function returns.

Like havman56 said, you MUST set VC++ to work with multi-threaded libraries. You can set it to debug/release mode.
During developing, it is recommended to work in debug mode
as the system will more easily catch any error you made.

Hope it helps.
0
 
rajeev_devinCommented:
Here is a very popular Reader/Writer problem solution. The Readed routine loads the buffer with that data, and the writer routine prints the data in the console. Just go through the code and experiment with it. You will learn a lot.

char buffer[10]; // Buffer
char *data[4] = {"this","is","a","test"}; // Data

DWORD WINAPI Reader(PVOID param)
{
     int count;
     count = *((int *)param);

     for(int i=0;i<count;++i)
     {
          strcpy(buffer,data[i]);
          Sleep(1);
     }
     return(0);
}
DWORD WINAPI Writer(PVOID param)
{
     int count;
     count = *((int *)param);
     for(int i=0;i<count;++i)
     {
          printf("\n%s",buffer);
          Sleep(1);
     }
     return(0);
}

void main()
{
     DWORD retvalue;
     int count=4;
     HANDLE h1 = CreateThread(0,0,Reader,&count,0,&retvalue);
     HANDLE h2 = CreateThread(0,0,Writer,&count,0,&retvalue);
     getch();
}


Hope this may help you
0
 
kaushik787Author Commented:
that was best way anybody could explain,mail me at kaushik31in@yahoo.com so that i can catch in future
if i have any problems
0
All Courses

From novice to tech pro — start learning today.