I would like to create a thread passing a vector as parameter.
but i got the following errors:
error: invalid conversion from ‘int’ to ‘void* (*)(void*)’ [-fpermissive] error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’ [-fpermissive]
I have the following code:
#include <iostream>
#include <vector>
#include <pthread.h>
using namespace std;
void* func(void* args)
{
vector<int>* v = static_cast<vector<int>*>(args);
cout << "Vector size: " << v->size();
}
int main ( int argc, char* argv[] )
{
vector<int> integers;
pthread_t thread;
for ( int i = 0; i < 10; i++)
integers.push_back(i+1);
// overheat call
//pthread_create( &thread, NULL, func, static_cast<void*>(&integers));
pthread_create( &thread, NULL,func,&integers);
cout << "Main thread finalized" << endl;
return 0;
}
How I can do it properly ?
Thanks
EDIT: forgot the includes posting here only; Revised.
I got new errors:
error: stray ‘\305’ in program error: stray ‘\231’ in program
I am trying to know about it.
Thanks in advance.
FINAL EDIT : Thanks to all. Sorry, I had another int var called func in other location. Thanks for your help.
You have forgotten to include
<vector>; this confuses the compiler as it first fails to generatefunc, and then fails to identify it as a function in the call topthread_create.Once you include that, your code should compile (and you can remove the
static_cast<void*>if you like); but to work correctly you also need to callpthread_joinbefore the vector goes out of scope, and return a value fromfunc.UPDATE: your latest edit has broken the code: you should not cast
functovoid*, but leave it as a function pointer. This should work:Errors like
stray ‘\305’ in programimply that you have some strange characters in your code, although they’re not in the code you’ve posted. Have a look at the lines that the error messages refer to.