为什么我的多线程程序无法正常终止?

时间:2016-10-23 21:26:07

标签: c++ multithreading

我编写了一个多线程程序(我认为)可以正常工作,但它似乎没有正确终止。大多数情况下它似乎运行并且没有提供任何错误(但不会以返回值终止)但是偶尔会无法打印所有循环并产生运行时错误。我对多线程很新,但或多或​​少遵循this guide

我在这里缺少什么?我假设终止线程是一种捕获,但据我所知,我已经覆盖了pthread_exit。请看一下,如果有任何问题,请告诉我。谢谢!

#include <iostream>
#include <time.h>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <pthread.h>
#include <string>

using namespace std;

struct threadData {
    int     thread_id;
    int*    tArray;
    int     tArraySize;
    int     tQuery;
};

//this is included because for whatever reason the std::to_string fxn is not recognized by my compiler
template <typename T>
std::string to_string(T value)
{
      //create an output string stream
      std::ostringstream os ;

      //throw the value into the string stream
      os << value ;

      //convert the string stream into a string and return
      return os.str() ;
}

//Counts the number of times the query is in the array
int countInstance(int* array, int arraySize, int query)
{
    int numInstance = 0;

    for ( int i = 0; i < arraySize; i++) 
    {
        if (array[i] == query) 
            numInstance += 1;
    }

    return numInstance;
}

void *threadFunction(void *threadArg)
{
    struct threadData *thisThread;

    thisThread = (struct threadData *) threadArg;

    //Originally wanted to use this to print each string, but it seemed to have issues printing correctly due to simultaneous threads
    /*cout << "Query: "         << thisThread->tQuery
         << "\tCount:  "    << countInstance(thisThread->tArray, thisThread->tArraySize, thisThread->tQuery) 
         << "\tThreadID: "  << thisThread->thread_id << endl;*/

    //Decided to create a concatenated string of the desired phrase instead.
    string queryString = to_string(thisThread->tQuery);
    string instanceString = to_string(countInstance(thisThread->tArray, thisThread->tArraySize, thisThread->tQuery));
    string idString = to_string(thisThread->thread_id);
    string outputString = "Query: " + queryString + "\tCount:  " + instanceString + "\tThreadID: " + idString + "\n";

    cout << outputString;

    pthread_exit(NULL);
}

int main(void)
{   
    int arraySize = 1000;
    int numArray[arraySize];

    srand(time(NULL));

    //Populate array with random values ranged [0, 100]
    for ( int i = 0; i < arraySize; i++) 
    {
        numArray[i] = (rand() % 101);
    }

    vector<int> numList;

    numList.push_back(1);
    numList.push_back(3);
    numList.push_back(5);
    numList.push_back(7);

    pthread_t threads[numList.size()];
    struct threadData data[numList.size()];
    int rc;

    for (int i = 0; i < numList.size(); i++)
        {
            data[i].tArray = numArray;
            data[i].tArraySize = arraySize;
            data[i].thread_id = i;
            data[i].tQuery = numList[i];
            rc = pthread_create(&threads[i], NULL, threadFunction, (void *)&data[i]);
            if (rc) 
                exit(-1);
        }
    pthread_exit(NULL);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

不应该调用join,而是应该放置一个for (int i=0; i<numlist.size(); i++) { void *rv; pthread_join(threads[i], &rv); } 次调用循环来等待线程完成:

pthread_exit

ftp_get ( resource $ftp_stream , string $local_file , string $remote_file , int $mode [, int $resumepos = 0 ] ) 用于与主线程不同的线程中,以便在自然终止之前中止它。