私はマルチスレッドのプログラムを書いたことがありますが、それは正常に終了するとは思われません。ほとんどの場合、実行されてエラーを出さないようですが(戻り値で終了しない)、すべてのループを出力できず、実行時エラーが発生します。私はマルチスレッドにはまだまだ従っていますが、多かれ少なかれ続きます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;
}
threadFunction()に渡す* threadArg *ポインタが迷惑メールです。これは、スレッドが実行されるまでには*もはや実行されていないかもしれない関数のローカル変数です。より永続的な場所を探してデータを保存します。一般的な火災と忘れスレッドのバグbtw。 –
@HansPassant、ここで推奨される解決策は何ですか?私はその構造体にデータを格納してはいけませんか? – Brendan