如何将void指针矢量数组转换为矢量数组?
vector<string>* vArray = new vector<string[numThreads];
p_thread_create(&my_thread[1], NULL, &function, (void)* vArray[1]);
void* function (vector<string> vArray[])
{
//?? casting?
}
当作为参数传递时(void)* vArray [1]它表示无效的演员...... 如果函数有2个参数,那么pthread可以创建更多参数,例如2吗?感谢
这是我的完整代码
#include <iostream>
#include <iomanip>
#include <iterator>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <pthread.h>
#include <queue>
using namespace std;
int array[5] = { 0, 0, 0, 0, 0 };
void* readData(void* arg)
{
string fileName = "Wisconsin.txt";
cout << "HERE!"<< endl;
ifstream cities(fileName.c_str());
string line;
while(!cities.eof()){
getline(cities, line);
if(line != "") {
int commaPos = line.find(',');
int popul = atoi(line.substr(commaPos + 2).c_str());
int x = popul;
if (x >= 1 && x <= 999)
{
array[0]++;
} else
if (x >= 1000 && x <= 9999)
{
array[1]++;
} else
if (x >= 10000 && x <= 99999)
{
array[2]++;
} else
if (x >= 100000 && x <= 999999)
{
array[3]++;
} else
if (x >= 1000000)
{
array[4]++;
}
} // end of IF
} // end of while
}
int main()
{
int numThreads;
ifstream ifs("states.txt");
std::string str((std::istreambuf_iterator<char>(ifs)),
std::istreambuf_iterator<char>());
stringstream ss(str);
int fileCount = 0;
string fileName;
while (ss >> fileName)
{
fileCount++;
}
cout << fileCount;
string* filesArr = new string[fileCount];
ss.clear();
ss.seekg(0, std::ios::beg);
for (int i = 0; i < fileCount; i++)
{
ss >> filesArr[i];
}
cout << "Enter number of threads: ";
cin >> numThreads;
vector<string>* vArray = new vector<string>[numThreads];
for (int i = 0; i < fileCount; i++)
{
vArray[i % numThreads].insert(vArray[i % numThreads].begin(), filesArr[i]);
}
//queue<string>* queueArr = new queue<string>[numThreads];
//for (int i = 0; i < fileCount; i++)
// queueArr[i % numThreads].push(filesArr[i]);
for(int i = 0; i < numThreads; i ++)
{
cout << endl;
cout << "FOR THREAD " << i + 1 << ":" << endl;
for (std::vector<string>::iterator it = vArray[i].begin(); it != vArray[i].end(); it++)
{
cout << *it << endl;
}
}
pthread_t my_thread[numThreads];
int ret = pthread_create(&my_thread[1], NULL, &readData, (void*)(vArray+1));
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
///////////// ERROR HERE!
pthread_join(my_thread[1], ret);
// for (int id = 0; id < numThreads; id++)
// {
//}
pthread_join(my_thread[1]);
cout << endl;
printf("%-20s", "1-999:");
cout << array[0] << endl;
printf("%-20s", "1,000 - 9,999:");
cout << array[1] << endl;
printf("%-20s", "10,000-99,999:");
cout << (array[2]) << endl;
printf("%-20s", "100,000-999,999:");
cout << (array[3]) << endl;
printf("%-20s", "1,000,000+:");
cout << (array[4]) << endl;
//int pos;
//string token;
//while ((pos = s.find(delimiter))
cin.get();
return 0;
}
pthread join现在似乎没有工作..编译它返回我:函数'int pthread_join(pthread_t,void **)的参数太少
答案 0 :(得分:2)
清理完代码后:
#include <iostream>
#include <vector>
using namespace std;
const int numThreads = 10;
void * thread_body (void * param)
{
// retrieve parameter
string& prm = *(string *)param;
// use it
cout << prm << endl;
return NULL;
}
int main() {
vector<string >threadPrm(numThreads);
vector<pthread_t>threadId (numThreads);
threadPrm[1] = "Hello";
pthread_create(&threadId[1], NULL, thread_body, &threadPrm[1]);
pthread_join (threadId[1], NULL);
return 0;
}
你似乎对
感到满意此外,尝试使用静态强制转换关闭编译器是生成愚蠢代码的可靠方法 关键是通常使代码运行,而不是不惜一切代价编译它。
最后,您可以通过reading the manual
回答您自己关于posix线程可以采用的参数数量的问题。