简体   繁体   English

pthread_create将动态矢量数组传递给参数

[英]pthread_create passing in dynamic vector array into argument

how do I cast a void pointer vector array into a vector array? 如何将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?
}

when passed in as argument (void)* vArray[1] it says invalid cast... And can pthread create take in more arguments, say 2, if the function takes 2 parameters? 当作为参数传递时(void)* vArray [1]它表示无效的转换...并且如果函数采用2个参数,那么pthread可以创建更多的参数,比如说2吗? Thanks 谢谢

Here's my complete code 这是我的完整代码

#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 doesn't seem to be working now.. compiling it return me: too few arguments to function 'int pthread_join(pthread_t, void**) pthread join现在似乎没有工作..编译它返回我:函数'int pthread_join(pthread_t,void **)的参数太少

After cleaning up your code: 清理完代码后:

#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;
}

You don't seem to be comfortable with 你似乎并不舒服

  • vectors 矢量
  • pointers and references 指针和参考
  • variable naming 变量命名

Also, trying to shut the compiler up with static casts is a sure way to produce silly code. 此外,尝试使用静态强制转换关闭编译器是生成愚蠢代码的可靠方法。
The point is usually to make the code run, not to compile it at all cost. 关键是通常使代码运行,而不是不惜一切代价编译它。

Lastly, you can answer your own question about the number of parameters a posix thread can take by reading the manual 最后,您可以通过阅读手册回答您自己关于posix线程可以采用的参数数量的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM