パラメータとしてベクトルを渡すスレッドを作成したいと考えています。 しかし、私は次のエラーを得た:C++でベクトル<int>をpthread_createに渡すにはどうすればよいですか?
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]
私は次のコードを持っている:
#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;
}
私はそれを正しく行うことができますどのように? ありがとう
編集:忘れていたここに掲載しています。改訂されました。
私は新しいエラーを得た:
error: stray ‘\305’ in program error: stray ‘\231’ in program
私はそれについて知っているしようとしています。
ありがとうございます。
FINAL EDIT : Thanks to all. Sorry, I had another int var called func in other location. Thanks for your help.
エラーは第3引数であり、第4引数ではありません。エラーを再現するための正確なコードを貼り付けていますか?コンパイラは 'func'が何であるか分からないので、それをintとして扱います。 – stijn
キャスティングについて... 'pthread_create'の呼び出しでベクトルをキャストする必要はありませんが、スレッド関数の代わりに 'reinterpret_cast'を使用する必要があります。 –
@stijnはい、それは正確なコードです、私はfuncをキャストしようとしています。私はなぜコールバックを認識しないのかわかりません。 – ppk