views:

619

answers:

5

What is thread? How to create thread in win32 application?

+1  A: 

A thread is the context that currently occupies the CPU and is the part that is scheduled by Windows CE.

To create a thread use CreateThread. You can read about more threading and process functions here.

This information is correct for Windows CE 6 as well.

Shaihi
A: 

Hi. Very popular is explained in Wikipedia :)

http://en.wikipedia.org/wiki/Thread%5F%28computer%5Fscience%29

What about how to handle it, you can read by ex the

.NET multithreading (Alan Dennis) isbn=1930110545

serhio
A: 

Thread is a light weight process. A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently of everything else that might be happening. A thread is like a classic program that starts at point A and executes until it reaches point B. It does not have an event loop. A thread runs independently of anything else happening in the computer. Without threads an entire program can be held up by one CPU intensive task or one infinite loop, intentional or otherwise. With threads the other tasks that don't get stuck in the loop can continue processing without waiting for the stuck task to finish. Please go through this link for more detail and its comparision with process.

http://en.wikipedia.org/wiki/Thread%5F%28computer%5Fscience)

Creating thread is very easy for an example go through this....

This is a very example which creates thread i.e ThreadFun1

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

void __stdcall ThreadFun1()
{
    printf("Hi This is my first thread.\n");
}
void main()
{
    printf("Entered In Main\n");
    HANDLE hThread;
    DWORD threadID;
    hThread = CreateThread(NULL, // security attributes ( default if NULL )
          0, // stack SIZE default if 0
          ThreadFun1, // Start Address
          NULL, // input data
          0, // creational flag ( start if  0 )
          &threadID); // thread ID
    printf("Other business in Main\n"); 
    printf("Main is exiting\n");
    CloseHandle(hThread);
    getch();
}
Abhi
How to create two threads ?
which calls the same function?
+2  A: 

Don't use CreateThread(), use _beginthreadex() instead if you are writing C/C++ programs.

_beginthreadex() will initialize the C/C++ runtime, but CreateThread() won't.

doudehou
A: 

All these answers suggest to use CreateThread()

That is simply poor advice.

Threads should generally be created with _beginthread() or _beginthreadex() to ensure the C/C++ runtime thread-local structures are appropriately initialised.

See the discussion on this question for further details: http://stackoverflow.com/questions/331536/windows-threading-beginthread-vs-beginthreadex-vs-createthread-c

polyglot
That would be true had been a full Windows OS. Unfortunately, the OS at question is Windows CE. It does not have `_BeginThread`.
Shaihi
Why are you discussing Windows CE? Where does the person asking the question raise any issue with Windows CE? _beginthread() is not an operating system call, it is a wrapper around CreateThread() and is available for use by the C/C++ runtime. Just like malloc() and free()
polyglot