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
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, TIME); // wait for thread to exit, TIME is a DWORD in milliseconds
[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.
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.
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.
You probably want to take a look at the CreateThread() function.
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.
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.
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.
Related
I'm trying to understand what the difference is between CreateThread and _beginthreadex and why calling DisableThreadLibraryCalls only prevents threads installed with _beginthreadex from executing.
I have a project which is a DLL, an old DLL, this is a win32 dll and I am tasked with porting it to win64. One thing that tripped me up was a call in DLLMain to DisableThreadLibraryCalls.
My threads were installed with _beginthreadex, the body of the threads was never being executed cause of the call to DisableThreadLibraryCalls. Once I removed this, the threads were working correctly.
Now I've found that other threads in the same DLL are started with CreateThread, I then thought was the call to DisableThreadLibraryCalls there to prevent these threads from executing so I put it back and found that the threads created with CreateThread are executed regardless of if DisableThreadLibraryCalls is present or not but threads created with _beginthreadex are disabled.
Why? I can't find anything on:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682579(v=vs.85).aspx
That describes why this is happening.
CreateThread() is a Windows native API for creating threads while _beginthread() and _beginthreadex() are part of C runtime library and are intended to make it easier to manage thread creation, but they still internally have to call CreateThread().
Your own answer is wrong because there is no explicit checking for enable/disable involved in C runtime.
Instead, C runtime library uses DLL_THREAD_ATTACH and DLL_THREAD_DETACH callbacks to manage thread local storage so it should not be surprising that disabling those callbacks by calling DisableThreadLibraryCalls() is preventing C runtime thread management functions from working correctly.
I have found references online that state that _beginthread and _beginthreadex interally call CreateThread so perhaps the checking disable/enable status is only performed in the top level routines which does echo my findings and to me suggests missing logic in CreateThread.
DisableThreadLibaryCalls has no effect on threads created with CreateThread directly.
When a new window is created using CreateEx does its code execute in its own thread or that of its parent (i.e. the thread in which the its instantiating code was executed)? Thanks.
The window doesn't actually run any code on its own, all the code is called from the message loop which is part of your own code. You can run into huge issues trying to interact with the Windows UI with multiple threads, so you should always respond to the messages within a single thread.
CreateWindowEx() does not create a new thread. If you want a new thread you have to call
either _beginthreadex() (usually preferred) or CreateThread().
In case you're wondering, _beginthreadex() is preferred over CreateThread() because the former initializes parts of the CRT that the latter does not.
Windows have thread affinity – see Raymond Chen's article on this matter.
No, create window dont start new thread
Cross-thread GUI stuff usually ends in disaster. The windows libraries actively discourage it by throwing exceptions.
Even if it was allowed, CreateWindowEx could not do this by default because it would be making some very big assumptions about your code (locks, thread safety, etc); and most Windows development is probably still essentially single threaded.
When I write C/C++ code for Windows platform, I usually use Windows APIs as necessary. But when it comes to multi-threading, I read the following quotaion from < Windows via C/C++ >
The CreateThread function is the
Windows function that creates a thread.
However, if you are writing C/C++
code, you should never call
CreateThread. Instead, you should use
the Microsoft C++ run-time library
function _beginthreadex. If you do not
use Microsoft's C++ compiler, your
compiler vendor will have its own
alternative to CrateThread. Whatever
this alternative is, you must use it.
AFAIK, a language run-time library for a certain platform is implemented with that platform's APIs. I think it is totally possible to call CreateThread() from my C/C++ code. And I quite did that. But I just don't understand why the above rule should be followed.
Many thanks for your insights.
Of course it's possible to use the Windows API's CreateThread directly.
But that leaves the runtime library uninformed about the new thread.
For the multi-threading support in the runtime library (and that includes functions that rely on static storage, e.g. I imagine it includes strtok) you need to keep the runtime library informed, and not only informed, but partially in charge so that e.g. failure to allocate whatever resources it needs for a thread, results in thread creation failure.
Cheers & hth.,
The c-runtime has numerous stateful variables that hold things, such as the current locale. These values have to be settable per thread otherwise code in oen thread (e.g. calling setlocale) could unduly influence code running in a different thread.
_beginthread wraps your thread in code that performs the necessary allocation AND deallocation of these per thread data structures.
If you call CreateThread directly, the structures will probably be allocated as required, but without the wrapper, the runtime will never know when the thread exits and they will leak.
I am wondering whether to use beginthread or QueueUserWorkItem for threaded methods in C++. What are the differences between the two APIs and in what context are they better suited?
Thanks,
BTW, I have read this question Windows threading: _beginthread vs _beginthreadex vs CreateThread C++
QUWI uses a thread from the thread pool to execute the callback function. Such threads are very light weight but not suitable for all types of threaded tasks. Basic requirements are that they need to be relatively short-lived, don't block very often and are not time critical.
It is all rather well explained in the SDK topic.
In C# there is a method SetApartmentState in the class Thread.
How do I do the same thing in C++?
For unmanaged processes, you control the apartment model used for a thread by passing appropriate parameters to CoInitializeEx(). Larry Osterman wrote up a great little guide to these:
...
When a thread calls CoInitializeEx (or
CoInitialize), the thread tells COM
which of the two apartment types it’s
prepared to host. To indicate that
the thread should live in the MTA, you
pass the COINIT_MULTITHREADED flag to
CoInitializeEx. To indicate that the
thread should host an STA, either call
CoInitialize or pass the
COINIT_APARTMENTTHREADED flag to
CoInitializeEx.
...
-- http://blogs.msdn.com/larryosterman/archive/2004/04/28/122240.aspx
c++ doesn't have built in thread support. What you are looking for depends on how you are implementing threads in your application. Win32? pthreads? boost::threads? Whichever API you are using will determine the answer to your question.
EDIT: looks like this may have an example for you: http://msdn.microsoft.com/en-us/library/system.threading.apartmentstate.aspx
It looks like it applies to managed c++.