CreateThread in vc++

Posted on 2003-02-24
Medium Priority
Last Modified: 2012-05-04
i'm a beginner can somebody help me with CreateThread:

LPVOID lpParameter.

also help for CreateProc.        
Question by:kaushik787
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

havman56 earned 200 total points
ID: 8014754

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");
     // _getch();
     // CloseHandle( hInputThread );


 DWORD WINAPI InputFunc( LPVOID lpParam )

   return 0;

Expert Comment

ID: 8015315
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.
LVL 12

Expert Comment

ID: 8015437
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

     int count;
     count = *((int *)param);

     for(int i=0;i<count;++i)
     int count;
     count = *((int *)param);
     for(int i=0;i<count;++i)

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);

Hope this may help you

Author Comment

ID: 8015454
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question