views:

939

answers:

7
  • I tend to use POSIX Threads, when programming in C, under Linux.

  • Without MFC

Question:

How would I then create threads in VC++?

Find more information on threads under win32?

Edit:

  • Brief illustrations

I LOVE stackoverflow - best resource for students!

Regards

+1  A: 

Some good books on the subject are Petzold's Programming Windows and Richter's Programming Applications for Windows. In particular, the latter goes into server side programming such as thread and synchronisation API's in a lot of depth.

EDIT: For code snippets, Google is your friend. For example This article has some minimal thread examples.

ConcernedOfTunbridgeWells
T@NXC: Thanks! I'll edit the question. I was looking, also for a brief illustration of simple thread creation from main()
Aaron
Google is your friend. For example http://www.codeproject.com/KB/threads/crtmultithreading.aspx has some minimal thread creation examples.
ConcernedOfTunbridgeWells
@NXC: THANKS :) - I LOVE stackoverflow!!! Great resource for students :)
Aaron
+1  A: 

You probably want to take a look at the CreateThread() function.

Andy
No, you actually want to use _beginthread() or _beginthreadex(). See this question for why: http://stackoverflow.com/questions/331536/windows-threading-beginthread-vs-beginthreadex-vs-createthread-c
Adam Rosenfield
+7  A: 

if you're looking for a platform-independent method, use boost

there's also beginthread() and beginthreadex() functions. Both seem to be supplemental to Win32 API, in a sense that in many use cases, you still need to call some Win32 functions (such as CloseHandle for beginthreadex). So, if you don't care that much about platform compatibility, you might as well cut the foreplay and use CreateThread().

Win32 thread handling is documented here: http://msdn.microsoft.com/en-us/library/ms684852(VS.85).aspx

[edit1] example:

DWORD WINAPI MyThreadProc( void* pContext )
{
    return 0;
}

HANDLE h = CreateThread( NULL, 0, MyThreadProc, this, 0L, NULL );
WaitForSingleObject(h); // wait for thread to exit

[edit2] CRT & CreateThread():

per MSDN:

A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.

galets
No, don't cut the foreplay. CreateThread() does not play nice with the C runtime.
Adam Rosenfield
CreateThread() is fine - I'm not looking for platform-independents
Aaron
Sorry, I wasn't very clear there. See this question http://stackoverflow.com/questions/331536/windows-threading-beginthread-vs-beginthreadex-vs-createthread-c
Adam Rosenfield
A: 

Use _beginthread() or _beginthreadex() to create a new thread. Do NOT use the Win32 function CreateThread() -- it does not properly initialize the multithreaded aspects of the C runtime. See also this question.

Adam Rosenfield
A: 

There is also the _beginthread() function you can look up. It differs a bit from CreateThread(), you should be aware of the differences before choosing one.

Philibert Perusse
+1  A: 

You should not use the raw Win32 CreateThread() API.

Use the C runtime's _beginthreadex() so the runtime has an opportunity to set up its own thread support.

Michael Burr
@Michael Burr: perfect :)
Aaron
This is no longer true.
Hans Passant
+2  A: 

You can use either the CRT function _beginthreadex() or the Windows API function CreateThread(). _beginthreadex() is required for early versions of VC++ that had a CRT that didn't lazily initialize thread-local storage. CreateThread() is fine in at least VS2005 and up.

Hans Passant
Interesting. Could you elaborate on the lazy initialisation of TLS and why CreateThread is now OK? I always used _beginthreadex thinking this was necessary for correct CRT initialisation.
ChrisN
It's a dead thread. Why don't you start a new one, like "Is _beginthreadex() still necessary?"
Hans Passant