[英]c++ - Passing vector to thread
我在通過pthread_create傳遞矢量時遇到如下錯誤,如下所示。
#include <iostream>
#include <pthread.h>
#include <vector>
using namespace std;
void foo(void *a)
{
vector <int> b = (vector <int>*)a;
for(int i=0; i<b.size(); i++)
{
std::cout<<b[i];
}
return NULL;
}
void bar(int x)
{
std::cout<<"bar";
}
int main()
{
pthread_t thr;
std::vector <int> a = {1,2,3};
pthread_create(&thr, NULL, &foo, (void *)&a);
pthread_join(thr,NULL);
return 0;
錯誤訊息:
threadeg.cpp: In function ‘void foo(void*)’:
threadeg.cpp:9:35: error: conversion from ‘std::vector<int>*’ to non-scalar type ‘std::vector<int>’ requested
vector <int> b = (vector <int>*)a;
threadeg.cpp:16:10: error: return-statement with a value, in function returning 'void' [-fpermissive]
return NULL;
^
threadeg.cpp: In function ‘int main()’:
threadeg.cpp:28:46: error: invalid conversion from ‘void (*)(void*)’ to ‘void* (*)(void*)’ [-fpermissive]
pthread_create(&thr, NULL, &foo, (void *)&a);
/usr/include/pthread.h:244:12: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’ [-fpermissive]
extern int pthread_create (pthread_t *__restrict __newthread,
我是線程的新手,我無法弄清楚出了什么問題。 誰能指出問題並提出解決方案?
首先,您會犯一些錯誤,即void函數不會返回任何內容。 在您的情況下,它返回NULL
。 根據定義, NULL
就是零。 因此,返回NULL
並不意味着什么也不返回。
bar()
函數是不必要的。
現在檢查工作代碼。
#include <iostream>
#include <pthread.h>
#include <vector>
using namespace std;
void *foo(void *a)
{
vector <int>* b = (vector <int>*)a;
for (auto it : *b) {
std::cout << it;
}
}
int main()
{
pthread_t thr;
std::vector <int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
pthread_create(&thr, NULL, &foo, &a);
pthread_join(thr,NULL);
return 0;
}
您將發送向量的地址。 而且,您將擁有一個void *
返回函數,而不是void
返回函數。 同樣,您將發送其地址。
我認為您想創建一個指針b而不是復制vector <int>* b = (vector <int>*)a;
幾乎沒有什么錯誤vector <int>* b = (vector <int>*)a;
。 函數foo簽名應為void* foo (void* a)
而不是void foo(void* a)
您應該將a
強制轉換為vector <int>* b
或如下所示的引用,因為要傳遞指針,然后嘗試將其分配給對象。
將foo
的返回值更改為(void*)
因為pthread_create
期望為void *(*start_routine) (void *)
。
#include <iostream>
#include <pthread.h>
#include <vector>
using namespace std;
void* foo(void *a)
{
const vector <int>& b = *(vector <int>*)a; // Cast to a reference or pointer, could be made const
for(int i=0; i<b.size(); i++)
{
std::cout<<b[i];
}
return NULL;
}
void bar(int x)
{
std::cout<<"bar";
}
int main()
{
pthread_t thr;
std::vector <int> a = {1,2,3};
pthread_create(&thr, NULL, &foo, (void *)&a);
pthread_join(thr,NULL);
return 0;
}
您可以在這里https://ideone.com/S1bWSk嘗試。
更新是的,如果可以的話,最好切換到std::thread
。 您可以通過引用傳遞數組。
void foo(const vector <int>& b)
{
for(int i=0; i<b.size(); i++)
{
std::cout<<b[i];
}
}
....
std::thread thr(foo, std::cref(a));
thr.join();
由於沒有答案(只是一個注釋),建議使用std::thread
代替POSIX函數,因此我想展示這種選擇。 它還解決/防止了文本中的許多其他錯誤,並且易於使用。
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
void foo(std::vector<int> const& b)
{
for(int i=0; i<b.size(); i++)
{
std::cout<<b[i];
}
}
void bar(int x)
{
std::cout<<"bar";
}
int main()
{
std::vector <int> a = {1,2,3};
std::thread thr(a, foo);
thr.join();
return 0;
}
值得一提。 創建線程對象時,將復制向量。 可以通過使用std::cref
或使用具有適當捕獲的lambda來避免這種情況。
需要時,您可以使用std::thread::native_handle()
獲取線程句柄,並將其傳遞給POSIX函數,例如pthread_getcpuclockid
:
std::thread thr(a, foo);
clockid_t clk;
pthread_getcpuclockid(thr.native_handle(), &clk);
我將封裝對遺留函數的調用, native_handle
在該封裝之外使用native_handle
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.