How address of a function will pass to the std::thread - c++

I was debugging a multithreaded application that is using std::thread to run a function. I reach to the following code when debugging.
extern "C" uintptr_t __cdecl _beginthreadex(
void* const security_descriptor,
unsigned int const stack_size,
_beginthreadex_proc_type const procedure,
void* const context,
unsigned int const creation_flags,
unsigned int* const thread_id_result
)
{
_VALIDATE_RETURN(procedure != nullptr, EINVAL, 0);
unique_thread_parameter parameter(create_thread_parameter(procedure, context));
if (!parameter)
{
return 0;
}
DWORD thread_id;
HANDLE const thread_handle = CreateThread(
reinterpret_cast<LPSECURITY_ATTRIBUTES>(security_descriptor),
stack_size,
thread_start<_beginthreadex_proc_type, true>,
parameter.get(),
creation_flags,
&thread_id);
if (!thread_handle)
{
__acrt_errno_map_os_error(GetLastError());
return 0;
}
if (thread_id_result)
{
*thread_id_result = thread_id;
}
// If we successfully created the thread, the thread now owns its parameter:
parameter.detach();
return reinterpret_cast<uintptr_t>(thread_handle);
}
But I couldn't understand how address of the function passed to the CreateThread API. Why is it using thread_start<_beginthreadex_proc_type, true>, and how the address of a function will calculate by this statement in order to run by threads?

The code shown (_beginthreadex function) is part of VC++ CRT (can be found in, e.g.
C:\Program Files (x86)\Windows Kits\10\Source\10.0.17763.0\ucrt\startup\thread.cpp).
An instance of unique_thread_parameter is a structure that holds the thread procedure pointer, the context argument, and thread and module HANDLEs:
// corecrt_internal.h
typedef struct __acrt_thread_parameter
{
// The thread procedure and context argument
void* _procedure;
void* _context;
// The handle for the newly created thread. This is initialized only from
// _beginthread (not _beginthreadex). When a thread created via _beginthread
// exits, it frees this handle.
HANDLE _thread_handle;
// The handle for the module in which the user's thread procedure is defined.
// This may be null if the handle could not be obtained. This handle enables
// us to bump the reference count of the user's module, to ensure that the
// module will not be unloaded while the thread is executing. When the thread
// exits, it frees this handle.
HMODULE _module_handle;
// This flag is true if RoInitialized was called on the thread to initialize
// it into the MTA.
bool _initialized_apartment;
} __acrt_thread_parameter;
// thread.cpp
using unique_thread_parameter = __crt_unique_heap_ptr<
__acrt_thread_parameter,
thread_parameter_free_policy>;
create_thread_parameter creates such an instance:
static __acrt_thread_parameter* __cdecl create_thread_parameter(
void* const procedure,
void* const context
) throw()
{
unique_thread_parameter parameter(_calloc_crt_t(__acrt_thread_parameter, 1).detach());
if (!parameter)
{
return nullptr;
}
parameter.get()->_procedure = procedure;
parameter.get()->_context = context;
// Attempt to bump the reference count of the module in which the user's
// thread procedure is defined, to ensure that the module will stay loaded
// as long as the thread is executing. We will release this HMDOULE when
// the thread procedure returns or _endthreadex is called.
GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
reinterpret_cast<LPCWSTR>(procedure),
&parameter.get()->_module_handle);
return parameter.detach();
}
thread_start is a template function that invokes the thread procedure:
template <typename ThreadProcedure>
static unsigned long WINAPI thread_start(void* const parameter) throw()
{
if (!parameter)
{
ExitThread(GetLastError());
}
__acrt_thread_parameter* const context = static_cast<__acrt_thread_parameter*>(parameter);
__acrt_getptd()->_beginthread_context = context;
if (__acrt_get_begin_thread_init_policy() == begin_thread_init_policy_ro_initialize)
{
context->_initialized_apartment = __acrt_RoInitialize(RO_INIT_MULTITHREADED) == S_OK;
}
__try
{
ThreadProcedure const procedure = reinterpret_cast<ThreadProcedure>(context->_procedure);
_endthreadex(invoke_thread_procedure(procedure, context->_context));
}
__except (_seh_filter_exe(GetExceptionCode(), GetExceptionInformation()))
{
// Execution should never reach here:
_exit(GetExceptionCode());
}
// This return statement will never be reached. All execution paths result
// in the thread or process exiting.
return 0;
}
It essentially calls invoke_thread_procedure, which just invokes procedure, passing in context:
static __forceinline unsigned int invoke_thread_procedure(
_beginthreadex_proc_type const procedure,
void* const context
) throw()
{
return procedure(context);
}
The code around the calls does some bookkeeping to keep the CRT consistent, for example it
automatically cleans up the thread upon exit (_endthreadex).

Related

An issue with arguments for detached thread

I am working on a school project (simulation of virtual memory), where we are supposed to use detached threads. There are also other limitations to what we can use but I will mention that later. The problem is that when I give the pthread_create function the last argument as (void*)something and the created thread is detached, the function called by pthread_create will recieve the argument, but since the thread is detached, the original arguments are deleted as soon as the function from where I called pthread_create finishes - that means the argument in the called function is no longer valid and therefore I get segmentation faults etc.. Here is a part of the code (cant post it whole, its very big):
bool CProcManager::NewProcess(void* processArg, void(*entryPoint)(CCPU*, void*), bool copyMem) {
uint32_t free_page;
pthread_attr_t attr;
pthread_t thread;
if (proc_cnt >= 63)
return 0;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (!FindFreePage(free_page)) return 0;
free_page = (free_page << 12) | 7;
CProcManager ccpu(m_MemStart, free_page);
ThrArgs args(entryPoint, (void *) processArg, ccpu);
proc_cnt++;
if (pthread_create(&thread, NULL, StartWork, (void *) &args))
return 0;
//pthread_mutex_lock(&start_mtx);
//pthread_cond_wait(&start_cond, &start_mtx);
//pthread_mutex_unlock(&start_mtx);
return 1;
}
Here you can see that this function gets a poitner to void and a pointer to function as parameters. pthread_creates tells the thread to call the "StartWork" function with argument "args", which is a structure containing a pointer to function, a pointer to void and an object of type CProcManager.
void* CProcManager::StartWork(void* arguments) {
//pthread_mutex_lock(&start_mtx);
ThrArgs *args = (ThrArgs*) arguments;
CProcManager ccpu = args->ccpu;
//pthread_cond_signal(&start_cond);
//pthread_mutex_unlock(&start_mtx);
args->entryPoint(&ccpu, args->processArg);
ccpu.RemovePage((ccpu.m_PageTableRoot >> 12) * PAGE_SIZE, false);
pthread_mutex_lock(&end_mtx);
proc_cnt--;
pthread_cond_signal(&end_cond);
pthread_mutex_unlock(&end_mtx);
return (NULL);
}
In this function, I finally call the function I had a pointer to. As you can guess, the arguments become invalid when the "NewProcess" function ends, so I tried to overcome this by adding a condition variable and somehow make a copy of the arguments I have (havent come to a solution yet) and then let the "NewProcess" function end. But there are certain limitations in this project. The function "NewProcess" is being called many times and the threads should run simultaneously (I tried signaling the condition variable after calling entryPoint and that worked), so I cant finish one thread and then do the next. The parameter "void* processArg" of the first function is originally a certain object type and this type cannot be accessed from the two functions above.
So can anyone please give me a suggestion how copy the arguments so I dont have a segmantation fault?
The problem is very simple and is one of object lifetimes and ownership.
You are passing the address of 'args' to the thread, but 'args' is created on the stack and goes out of scope soon after the thread has been started.
do it something like this:
// note: The ThrArgs constructor should take ownership of processArg
std::unique_ptr<ThrArgs> args (new ThrArgs(entryPoint, (void *) processArg, ccpu));
if (pthread_create(&thread, NULL, StartWork, (void *) args.get())) {
args.release();
// ownership of the pointer now belongs to the thread
return 0;
}
then in the thread function:
void* CProcManager::StartWork(void* arguments) {
std::unique_ptr<ThrArgs> args (reinterpret_cast<ThrArgs*>(arguments));
// now use args as a pointer
....
// the arguments will be deleted here
return 0;
}
There are two solutions. The simplest is often just to allocate
the arguments dynamically: in NewProcess:
ThrArgs* args = new ThrArgs( entryPoint, processArg, ccpu );
bool results = pthread_create( &thread, NULL, StartWork, args ) == 0;
if (!results) {
delete args;
}
(You could use std::unique_ptr or std::auto_ptr here as
well. In which case, don't call release on it unless
pthread_create succeeds!)
The alternative is to create a conditional variable over
a boolean, then wait on it before leaving the function, and copy
the parameters and then set the boolean in the new thread: in
NewProcess:
pthread_mutex_lock( &theMutex );
freeToContinue = false;
bool results = pthread_create( &thread, NULL, StartWork, args ) == 0;
if ( results ) {
while ( !freeToContinue ) {
pthread_cond_wait( &theCond, &theMutex );
}
pthread_mutex_unlock( &theMutex );
}
return results;
and in StartWork (the function you pass to pthread_create
must be extern "C", and so cannot be a member):
void* StartWork( void* arguments )
{
pthread_mutex_lock( &theMutex );
ThrArgs args = *static_cast<ThrArgs*>( arguments );
freeToContinue = true;
pthread_cond_broadcast( &theCond );
pthread_mutex_unlock( &theMutex );
// ...
}
(Obviously, I've left out a lot of error checking here.)
Either way: you may have to do something similar with regards to
the processArg argument, since its lifetime is determined by
that of the caller.
And you don't need all of the casts to void*.

GetCurrentThreadId returns different threadId

I call the mentioned windows API. But it returns a different thread id than the id returned by _beginthreadex. My code is following,
ThreadTest *_threadTest = new ThreadTest();
Thread *_thread = new Thread(StartRoutineForThread,_threadTest);
Constructor for Thread class is,
ThreadWin::ThreadWin(void * (*_startRoutine)(void *), void * _argument, bool _isJoinable)
{
unsigned int _threadAddress;
unsigned int threadID = _beginthreadex(
NULL,
0,
(unsigned int (__stdcall *)(void *))_startRoutine,
_argument,
0,
&_threadAddress
);
}
StartRoutineForThread function which is the start routine for thread is following,
void* StartRoutineForThread(void* _argument)
{
ThreadTest *_threadTest = (ThreadTest*)_argument;
_threadTest->Run();
return NULL;
}
void ThreadTest::Run()
{
this->threadID = ::GetCurrentThreadId();
}
Now in the constructor of class Thread the value of the variable threadID differs from the value of class ThreadTest's variablethreadID that I get from the Run function. But the Run function was called from the function that I specified when I have created the thread. So the Run function is running under the same thread that I have created. But then why GetCurrentThreadId() returns different value than what was returned by _beginthreadex ?
Well, _beginthreadex doesn't return thread id. Thread id is stored in _threadAddress, the last parameter of _beginthreadex. Its return value is thread handle (like CreateThread), not id.
According to MSDN, _beginthreadex returns thread handle, and it is not the same as thread ID.

passing one function to different threads

I have to create an application where I'll have to make multiple threads. SoI thought to try making one function and passing it to different threads. Initially I've created two threads and have declared one function to be passed to both of them. All I am trying to do is to pass different integers to those threads and display it in the thread function,here is my code:
DWORD WINAPI Name(LPVOID lpParam)
{
int *Ptr=(int*)lpParam;
for(int j=0;j<2;j++)
{
cout<<"Thread"<<endl;
cout<<*Ptr<<endl;
}
return 0;
}
int main()
{
int a=10,b=15,c=25;
HANDLE thread1,thread2;
DWORD threadID,threadID2;
thread2= CreateThread(NULL,0,Name,LPVOID(a),0,&threadID2);
thread1= CreateThread(NULL,0,Name,LPVOID(b),0,&threadID);
for(int i=0;i<5;i++)
{
cout<<"Main Thread"<<endl;
}
if(thread1==NULL)
{
cout<<"Couldn't Create Thread:("<<endl;
exit(0);
}
if(thread2==NULL)
{
cout<<"Couldn't Create Thread:("<<endl;
exit(0);
}
return 0;
}
but this code is not running properly,i.e compliles fine,starts fine but afterwards gives a debugging error.
Could someone let let me know of my mistake and how I could correct it coz being able to utilize one function for multiple threads will be really helpful for me.
Wait for your child threads to return. Do this:
int main()
{
int a=10,b=15,c=25;
HANDLE thread[2];
DWORD threadID,threadID2;
thread[1]= CreateThread(NULL,0,Name,LPVOID(a),0,&threadID2);
thread[0]= CreateThread(NULL,0,Name,LPVOID(b),0,&threadID);
for(int i=0;i<5;i++)
{
cout<<"Main Thread"<<endl;
}
if(thread[0]==NULL)
{
cout<<"Couldn't Create Thread:("<<endl;
exit(0);
}
if(thread[1]==NULL)
{
cout<<"Couldn't Create Thread:("<<endl;
CloseHandle(thread[0]);
exit(0);
}
WaitForMultipleObjects(2, thread, TRUE, INFINITE);
CloseHandle(thread[0]);
CloseHandle(thread[1]);
return 0;
}
The handle of a thread is signaled when the thread is terminated (refer CreateThread).
You are passing the address of a local variable in the function into your thread. By the time the thread gets around to running your function has probably exited main already. So the thread will try to access a variable that no longer exists on the stack so will be reading some random value which when you try to dereference it as a pointer will likely crash.
You main needs to wait. For a simple test just put in a Sleep(10000) or something before it exits. Obviously that's no use for a real program.
To summarize the comments: There are two ways you can pass data. Either directly inside the void pointer, because "void pointer" is an integral type and thus can represent integers (but it doesn't necessarily have the same width as int), or indirectly by passing an actual address of the thing you care about.
Method 1 (pass the value):
DWORD WINAPI Name(LPVOID lpParam)
{
intptr_t n = reinterpret_cast<intptr_t>(lpParam);
// ...
}
int main()
{
intptr_t a = 10;
thread1 = CreateThread(NULL, 0, Name, reinterpret_cast<void *>(a), 0, &threadID);
// ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
For this method, we use the integral type intptr_t, which has the same width as void *. We use reinterpret-casts to store and retrieve arbitrary integral values.
Method 2 (pass a pointer):
DWORD WINAPI Name(LPVOID lpParam)
{
T * p = static_cast<T *>(lpParam);
// ... use *p ...
}
int main()
{
T thing_I_care_about;
thread1 = CreateThread(NULL, 0, Name, &thing_I_care_about, 0, &threadID);
// ... ^^^^^^^^^^^^^^^^^^^
}
This is the more general method, but requires that thing_I_care_about remain alive, and it becomes a brittle shared state, so lifetime management and synchronisation become issues. Note that any object pointer is implicitly convertible to void *, so there's no need for the cast at the call site.
Finally, as others have commented, don't forget to join or detach your threads.

Close a thread when done with it

How do you close a thread, when you done? like making sure nothing is open anymore or runing?
so far i know how to open it, but .. not how to close it right
int iret1;
pthread_t thread1;
char *message1;
void *multithreading1( void *ptr ) {
while (1) {
// Our function here
}
}
int main (int argc, char * const argv[]) {
if( (iret1=pthread_create( &thread1, NULL, multithreading1, (void*) message1)) )
{
printf("Thread creation failed: %d\n", iret1);
}
return 0;
}
"How do you close a thread, when you done?"
Either by just simple returning from that function or calling pthread_exit function.
Note that calling return also causes the stack to be unwound and variables declared within start routine to be destroyed, thus it's more preferable than pthread_exit function:
An implicit call to pthread_exit() is made when a thread other than the thread in
which main() was first invoked returns from the start routine that was used to
create it. The function's return value shall serve as the thread's exit status.
For more information also have a look at: return() versus pthread_exit() in pthread start functions
"making sure nothing is open anymore or runing"
Instead of making sure whether your thread is still running, you should wait for its termination by using pthread_join function.
Here's an example:
void *routine(void *ptr) {
int* arg = (int*) ptr; // in C, explicit type cast is redundant
printf("changing %d to 7\n", *arg);
*arg = 7;
return ptr;
}
int main(int argc, char * const argv[]) {
pthread_t thread1;
int arg = 3;
pthread_create(&thread1, NULL, routine, (void*) &arg);
int* retval;
pthread_join(thread1, (void**) &retval);
printf("thread1 returned %d\n", *retval);
return 0;
}
output
changing 3 to 7
thread1 returned 7
To do this, you either return from the thread function (multithreading1) or call pthread_exit().
For more information, see POSIX Threads Programming.

C++: _beginthreadex, thread function name not showing in Visual Studio Threads window

I recently learnt that ::_beginthreadex() is always preferable to ::CreateThread(), so I changed all my calls that used ::CreateThread().
The only downside is that I no longer see the thread function's name in Visual Studio's Threads window making it hard to quickly identify threads. I assume this was somehow done automatically by the debugger when I used ::CreateThread(), since the parameters are exactly the same, I just changed the name of the function used.
Is there any way to keep using ::_beginthreadex() and to see the thread's name in the Threads window of Visual Studio?
This happens because _beginthreadex() calls CreateThread() with its own thread function that calls the one you specify (so the debugger uses the _threadstartex function name - the one that _beginthreadex() invokes).
You can manually set the thread name yourself using the SetThreadName() example from MSDN. What you might want to do is create your own wrapper for _beginthreadex() that maybe looks something like:
uintptr_t startthreadex(
void* security,
unsigned stacksize,
unsigned (__stdcall * threadproc) (void *),
void* args,
unsigned flags,
unsigned * pThread_id,
char* thread_name)
{
unsigned alt_thread_id;
if (!pThread_id) {
pThread_id = & alt_thread_id;
}
uintptr_t result = _beginthreadex(security, stacksize, threadproc, args, flgas, pThread_id);
if (result == 0) return result;
SetThreadName( *pThread_id, thread_name);
}
Now you can call startthreadex() instead of _beginthreadex() and pass it a thread name. A small advantage to this is that if you use the same function to run several threads, you can easily give them each unique names that reflect the parameters passed ot the thread or whatever.
If you want the thread name to automatically take be the thread proc function name as the debugger's thread name, you could make a wrapper macro that stringizes the function name parameter (all it takes is another level of indirection or to to solve any problem...).
Here's SetThreadName() (it's from http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx):
//
// Usage: SetThreadName (-1, "MainThread");
//
#include <windows.h>
const DWORD MS_VC_EXCEPTION=0x406D1388;
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
void SetThreadName( DWORD dwThreadID, char* threadName)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = threadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
__try
{
RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
}
}
There is no particular advantage with using _beginthreadex over CreateThread. The CRT functions would eventually call CreateThread only.
You should read:
Windows threading: _beginthread vs _beginthreadex vs CreateThread C++
http://www.codeguru.com/forum/showthread.php?t=371305
http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/c727ae29-5a7a-42b6-ad0b-f6b21c1180b2