簡體   English   中英

為什么我的多線程程序無法正確終止?

[英]Why is my multi-threaded program not terminating properly?

我已經編寫了一個多線程程序(我認為)可以正常工作,但是它似乎無法正確終止。 在大多數情況下,它似乎在運行並且不提供任何錯誤(但不以返回值終止),但有時它始終無法打印所有循環並給出運行時錯誤。 我是多線程技術的新手,但或多或​​少遵循了本指南

我在這里想念什么? 我認為終止線程是某種捕獲,但是據我所知,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;
}

而不是調用pthread_exit(NULL)您應該放置join調用循環以等待線程完成:

for (int i=0; i<numlist.size(); i++) {
    void *rv;
    pthread_join(threads[i], &rv);
}

pthread_exit用於與主線程不同的線程,以在其自然終止之前中止該線程。

暫無
暫無

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

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