簡體   English   中英

pthread_create將動態矢量數組傳遞給參數

[英]pthread_create passing in dynamic vector array into argument

如何將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 **)的參數太少

清理完代碼后:

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

你似乎並不舒服

  • 矢量
  • 指針和參考
  • 變量命名

此外,嘗試使用靜態強制轉換關閉編譯器是生成愚蠢代碼的可靠方法。
關鍵是通常使代碼運行,而不是不惜一切代價編譯它。

最后,您可以通過閱讀手冊回答您自己關於posix線程可以采用的參數數量的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM