Reputation: 1374
I'm attempting to do some basic parallelisation using _beginthreadex, and passing parameters as per an example I was given, but it won't work.
Any ideas?
#include <iostream>
#include <process.h>
void MyThread(void *data)
{
std::cout << "Hello World!";
}
int main()
{
_beginthreadex(NULL, 0, MyThread, NULL, 0, NULL);
while(true);
}
EDIT:
Why won't passing NULL as an argument work? (Since the function takes no arguments anyway?)
Passing NULL as an arguments list worked fine with _beginthread.
Upvotes: 1
Views: 20801
Reputation: 5728
The fourth parameter in _beginthreadex(NULL, 0, MyThread, NULL, 0, NULL) are arguments to the function MyThread
Right now you are passing NULL to it so void* data
must be getting a NULL pointer
struct X
{
int a;
int b;
};
X* x = (X*)malloc(sizeof(X));
x->a = 5;
x->b = 6;
_beginthreadex(NULL, 0, MyThread, NULL, x, NULL);
The above code will pass pointer x
to the function MyThread()
.
Do take care of X, as it better should be allocated by malloc or new. Do not create it on stack until you use a thread join
Upvotes: 0
Reputation: 68611
Your code has two errors in it, neither of which are related to the parameter to the thread function --- NULL
is fine for that, as you surmised.
The problems are in the signature of the thread function, and the error you are getting points this out. Firstly, it must be a __stdcall
function, and secondly it must return an unsigned int
. Your function is __cdecl
and returns void
.
unsigned __stdcall MyThread(void *data)
{
std::cout << "Hello World!";
return 0;
}
should fix the problem for you.
Upvotes: 7
Reputation: 30001
You pass your data through the fourth parameter. This passes a pointer to i
:
unsigned __stdcall thread(void *arg)
{
int *iptr = (int*)arg;
...
}
int i;
_beginthreadex(0, 0, thread, &i, 0, 0);
Note that the thread function signature I used here is different than what you use: I return an unsigned
and use the __stdcall
calling convention -- this is the signature _beginthreadex expects.
Depending on what you're trying to do, the new Concurrency Runtime features in VC++ might be simpler to use than explicitly managing your own threads.
Edit in response to question edit:
You can pass any valid void pointer, including NULL. If you do that, you can even leave out the parameter's name since you're not using it:
unsigned __stdcall thread(void*)
{
...
}
_beginthreadex(0, 0, thread, 0, 0, 0);
Upvotes: 1
Reputation: 35069
obviously in your code you are not passing any parameter. To pass a variable you have to do the following (for example):
#include <iostream>
#include <process.h>
void MyThread(void *data)
{
int x = static_cast<int*>(data);
std::cout << "Hello World! " << x;
}
int main()
{
int x = 10;
_beginthreadex(NULL, 0, MyThread, &x, 0, NULL);
while(true);
}
UPDATE: Since you posted the compilation problem later: apparently your thread function needs to return an integer:
int MyThread(void *data)
{
std::cout << "Hello World!";
return 0;
}
Upvotes: 6
Reputation: 146940
The argument to _beginthreadex must be a function with the __stdcall
calling convention. Your function has __cdecl
. A simple insertion of __stdcall
at the correct spot should solve the problem.
void __stdcall MyThread(void *data)
{
std::cout << "Hello World!";
}
Upvotes: 0