私のコードが異常な出力を出したり、時にはセグメント エラーが発生したりする理由がわかりません (私の大学が盗作をチェックした場合に備えて、このコードは私が 08218722 で作成しました)。
コードはLinux Mint 14のC++で記述され、次のようにコンパイルされています。
g++ -o code.exe code.cpp -lpthread
で実行し./code.exe
ます。
このコードは、最大 1000000 個のランダムな文字ベクトル (文字 a、b、c、d、e、および f のみを使用) を生成し、ループして各文字の数をカウントすることになっています。これは、スレッドを追加する前にうまく機能しました (非スレッドとスレッド間の実行時間をテストするための割り当ての一部)。そこで、ベクトルに追加する 1000 文字を作成するように指示された 1000 個のスレッドを作成します。ここで何が間違っていますか?
更新-コードはまだいくつかのクレイジーな結果を出力しています-今では文字ではなく、ある種の正方形のボックスが表示されます
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <vector>
#include <algorithm>
#include <pthread.h>
#include <ctime>
using namespace std;
//Variable Declarations
const int length = 1000000; // length
const int packets = 1000; // packets length
const char chars[6] = {'a', 'b', 'c', 'd', 'e', 'f'}; // chars to choose
vector<char> charList(length); //vector of random charecter list
int charGroup[6] = {}; //stores char count
pthread_t threads[packets];
//Structure Declarations
struct dataStruct {
int start;
int end;
};
//Function Declarations
void *randomLetterGenerator(void * args); // Declaring function
// prints the vector
void outVector(char n)
{
cout << n;
}
void GroupVector(char n)
{
charGroup[n] = charGroup[n] + 1;
}
int main(){
cout << "Creating a Random Char Array" << endl;
cout << "using only letters ranging between a and f." << endl;
//srand(time(NULL)); // sets the time seed
clock_t start = clock();
for(int i=0;i<length/packets;i++) {
printf("\rPlease wait...%3d/%3d",i*packets,length);
//Created in packets
dataStruct ds;
ds.start = i * (length/packets);
ds.end = ds.start + (length/packets);
pthread_create(&threads[i], NULL, randomLetterGenerator, (void *)&ds);
}
for(int i=0;i<length/packets;i++) {
pthread_join(threads[i], NULL);
}
printf("\n"); //new line
//prints out the new char list
for_each(charList.begin(), charList.end(), outVector) ;
printf("\n"); //new line
//Counts and places in the correct array
for_each(charList.begin(), charList.end(), GroupVector) ;
int total = 0;
for (int i = 0; i < 6; i++) {
total += charGroup[chars[i]];
cout << chars[i] << " = " << charGroup[chars[i]] << endl;
}
cout << "\nTotal: " << total << endl;
clock_t ends = clock();
cout << "Run Time :"
<< (double) (ends - start) / CLOCKS_PER_SEC << endl;
pthread_exit(NULL);
return 0;
}
void * randomLetterGenerator(void * datastruct){
dataStruct ds = *((dataStruct *) datastruct);
int start = ds.start;
int end = ds.end;
srand( time(NULL) );
for(unsigned int c=start;c<end;c++){
int i = (int) (rand() % 6);
char rchar = chars[i];
//--pthread_mutex_lock (&mutex);
charList.at(c)= i;
//--charList.push_back(rchar);
//--pthread_mutex_unlock (&mutex);
}
pthread_exit(NULL);
}