|
Creating Threads
The CreateThread function creates a new thread for a process. The creating thread must specify
the starting address of the code that the new thread is to execute. Typically,
the starting address is the name of a function defined in the program code.
This function takes a single parameter and returns a DWORD value. A process can
have multiple threads simultaneously executing the same function.
The following example demonstrates how to create a new thread that executes
the locally defined function, ThreadFunc.
DWORD WINAPI ThreadFunc( LPVOID lpParam )
{
char szMsg[80];
wsprintf( szMsg, "ThreadFunc: Parameter = %d\n", *lpParam );
MessageBox( NULL, szMsg, "Thread created.", MB_OK );
return 0;
}
VOID main( VOID )
{
DWORD dwThreadId, dwThrdParam = 1;
HANDLE hThread;
hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
ThreadFunc, // thread function
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
// Check the return value for success.
if (hThread == NULL)
ErrorExit( "CreateThread failed." );
CloseHandle( hThread );
}
For simplicity, this example passes a pointer to a DWORD value as an argument
to the thread function. This could be a pointer to any type of data or
structure, or it could be omitted altogether by passing a NULL pointer and deleting the
references to the parameter in ThreadFunc.
It is risky to pass the address of a local variable if the creating thread
exits before the new thread, because the pointer becomes invalid. Instead, either
pass a pointer to dynamically allocated memory or make the creating thread wait
for the new thread to terminate. Data can also be passed from the creating
thread to the new thread using global variables. With global variables, it is
usually necessary to synchronize access by multiple threads. For more information
about synchronization, see Synchronizing Execution of Multiple Threads.
In processes where a thread might create multiple threads to execute the same
code, it is inconvenient to use global variables. For example, a process that
enables the user to open several files at the same time can create a new thread
for each file, with each of the threads executing the same thread function. The
creating thread can pass the unique information (such as the filename)
required by each instance of the thread function as an argument. You cannot use a
single global variable for this purpose, but you could use a dynamically allocated
string buffer.
The creating thread can use the arguments to CreateThread to specify the following:
- The security attributes for the handle of the new thread. These security
attributes include an inheritance flag that determines whether the handle can be
inherited by child processes. The security attributes also include a security
descriptor, which the system uses to perform access checks on all subsequent uses
of the thread's handle before access is granted.
- The initial stack size of the new thread. The thread's stack is allocated
automatically in the memory space of the process; the system increases the stack as
needed and frees it when the thread terminates.
- A creation flag that enables you to create the thread in a suspended state.
When suspended, the thread does not run until the ResumeThread function is called.
Windows NT: You can also create a thread by calling the CreateRemoteThread function. This function is used by debugger processes to create a thread that
runs in the address space of the process being debugged.
| Last news from Greatis Software |
 |
|
Nostalgia .Net |
|
.Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes. More » |
| Recommended software for developers |
 |
|
Ultimate Pack |
|
Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price. More » |
 |
|
Form Designer .Net |
|
Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro! More » |
 |
|
Print Suite .Net |
|
Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available More » |
 |
|
Gradient Controls .Net |
|
Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available More » |
 |
|
Greatis iGrid |
|
iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors. More » |
All the contacts and projectsDmitry Vasiliev (just.dmitry)
Related LinksSoftware for Visual Studio .NET developers Software for Delphi and C++ Builder developers Software for Visual Basic 6 developers Delphi Tips&Tricks MegaDetailed.NET More Online Helps Win32 Programmer's Reference Win32 Multimedia Programmer's Reference OLE Programmer's Reference Microsoft Windows Pen API Programmer's Reference Microsoft Windows Sockets 2 Reference Microsoft Windows Telephony API (TAPI) Programmer's Reference Unix Manual Pages
|