Problem with Win32 Threads
-
Hello all, I´m trying to multithread an application by using CreateThread function. So far I´ve done the following:
//Here I initialize the Thread
LPDWORD iID;
HANDLE hThread; // array of thread handles
hThread = CreateThread (
0, // Security attributes
0, // Stack size
(LPTHREAD_START_ROUTINE)OpenCVWin32::Form1::Threadi,
NULL,
CREATE_SUSPENDED,
iID);
}//Called function
static DWORD WINAPI Threadi()
{
System::Windows::Forms::MessageBox::Show("Hi there!");
return 0;
}When I launch the application, the thread is not created, as I cannot go inside the Threadi function. I´m right now using VC++ 2008. Does anyone has a suggestion on why this could be happening? Thanks
-
Hello all, I´m trying to multithread an application by using CreateThread function. So far I´ve done the following:
//Here I initialize the Thread
LPDWORD iID;
HANDLE hThread; // array of thread handles
hThread = CreateThread (
0, // Security attributes
0, // Stack size
(LPTHREAD_START_ROUTINE)OpenCVWin32::Form1::Threadi,
NULL,
CREATE_SUSPENDED,
iID);
}//Called function
static DWORD WINAPI Threadi()
{
System::Windows::Forms::MessageBox::Show("Hi there!");
return 0;
}When I launch the application, the thread is not created, as I cannot go inside the Threadi function. I´m right now using VC++ 2008. Does anyone has a suggestion on why this could be happening? Thanks
kikoso wrote:
CREATE_SUSPENDED,
This is the problem. If you use this attribute then the thread is created in suspended state. More Info Here[^] If you want the thread to start as soon as you launch the application, don't create a thread in suspended state. If you want to create in suspended state, then you can use ResumeThread() function.
-
Hello all, I´m trying to multithread an application by using CreateThread function. So far I´ve done the following:
//Here I initialize the Thread
LPDWORD iID;
HANDLE hThread; // array of thread handles
hThread = CreateThread (
0, // Security attributes
0, // Stack size
(LPTHREAD_START_ROUTINE)OpenCVWin32::Form1::Threadi,
NULL,
CREATE_SUSPENDED,
iID);
}//Called function
static DWORD WINAPI Threadi()
{
System::Windows::Forms::MessageBox::Show("Hi there!");
return 0;
}When I launch the application, the thread is not created, as I cannot go inside the Threadi function. I´m right now using VC++ 2008. Does anyone has a suggestion on why this could be happening? Thanks
LPTHREAD_START_ROUTINE is defined as: typedef DWORD (WINAPI *PTHREAD_START_ROUTINE)( LPVOID lpThreadParameter ); typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE; so, your thread function is to be: static DWORD WINAPI Threadi(LPVOID lpThreadParameter) { System::Windows::Forms::MessageBox::Show("Hi there!"); return 0; }
-
LPTHREAD_START_ROUTINE is defined as: typedef DWORD (WINAPI *PTHREAD_START_ROUTINE)( LPVOID lpThreadParameter ); typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE; so, your thread function is to be: static DWORD WINAPI Threadi(LPVOID lpThreadParameter) { System::Windows::Forms::MessageBox::Show("Hi there!"); return 0; }
-
kikoso wrote:
CREATE_SUSPENDED,
This is the problem. If you use this attribute then the thread is created in suspended state. More Info Here[^] If you want the thread to start as soon as you launch the application, don't create a thread in suspended state. If you want to create in suspended state, then you can use ResumeThread() function.
Thank you HimanshuJoshi. I was reading in zig-zag the doc, so I didn´t realize about it. I still have some problems. After changing it, I have a problem accessing the memory. This is the error I got (after using 0 instead of CREATE_SUSPENDED,:
Unhandled exception at 0x003628c2 in OpenCV Win32.exe: 0xC0000005: Access violation reading at position 0x0000000c.
I´ve been thinking about sending the 3 parameter as (LPTHREAD_START_ROUTINE)&OpenCVWin32::Form1::Threadi, But I got a different error:error C3374: Address of "OpenCVWin32:: Form1:: Threadi" can only be adopted if a delegate instance is created.
I will appreciate any suggestions. Thank you :) -
Hello all, I´m trying to multithread an application by using CreateThread function. So far I´ve done the following:
//Here I initialize the Thread
LPDWORD iID;
HANDLE hThread; // array of thread handles
hThread = CreateThread (
0, // Security attributes
0, // Stack size
(LPTHREAD_START_ROUTINE)OpenCVWin32::Form1::Threadi,
NULL,
CREATE_SUSPENDED,
iID);
}//Called function
static DWORD WINAPI Threadi()
{
System::Windows::Forms::MessageBox::Show("Hi there!");
return 0;
}When I launch the application, the thread is not created, as I cannot go inside the Threadi function. I´m right now using VC++ 2008. Does anyone has a suggestion on why this could be happening? Thanks
If you're using MFC, you *must* use
AfxBeginThread
to begin a worker thread. If you are using Win32, you should be using_beginthreadex
to create a thread.CreateThread
is an API and that will even create a thread if you're linking to the single threaded version of the CRT (which will inevitably lead to disaster). If you're writing managed code, take a look atThread.Start
method in the documentation. Basically, I want to emphasise that there's no sane reason or excuse to useCreateThread
from your code. There are safer and sane alternatives.“Follow your bliss.” – Joseph Campbell
-
If you're using MFC, you *must* use
AfxBeginThread
to begin a worker thread. If you are using Win32, you should be using_beginthreadex
to create a thread.CreateThread
is an API and that will even create a thread if you're linking to the single threaded version of the CRT (which will inevitably lead to disaster). If you're writing managed code, take a look atThread.Start
method in the documentation. Basically, I want to emphasise that there's no sane reason or excuse to useCreateThread
from your code. There are safer and sane alternatives.“Follow your bliss.” – Joseph Campbell