gpt4 book ai didi

c - 如何在C中使用线程和互斥体?

转载 作者:行者123 更新时间:2023-11-30 20:36:57 26 4
gpt4 key购买 nike

我正在努力处理这段代码。这是关于从文本文件中计算一些模式。我尝试使用线程(分而治之)处理,但它返回错误的值。我使用互斥量值来同步关键部分。

第一个主要参数是线程数,第二个是我想要从中计算模式的文本文件的名称,以及我想要在文本中查找的以下模式。

代码如下

int *wordcount;
char *buffer;
int fsize, count;
char **searchword;
int *strlength;

typedef struct _params{
int num1;
int num2;
}params;

pthread_mutex_t mutex;

void *childfunc(void *arg)
{

int size, i, j, k, t, start, end, len, flag = -1;

int result;


params *a = (params *)arg;
start = a->num1;
end = a->num2;


pthread_mutex_lock(&mutex);

while(1){

if(start == 0 || start == fsize)
break;
if(buffer[start]!= ' ' && buffer[start] != '\n' && buffer[start] != '\t')
start++;
else
break;
}

while(1){

if(end == fsize)
break;
if(buffer[end] != ' ' && buffer[end] != '\n' && buffer[end] != '\t')
end++;
else
break;
}

for(i = 0; i < count; i++){

len = strlength[i];
for(j = start; j<(end - len + 1); j++){
if(buffer[j] == searchword[i][0]){

flag = 0;
for(k = j +1; k<j + len; k++){

if(buffer[k] != searchword[i][k-j]){
flag = 1;
break;

}

}
if(flag == 0){

wordcount[i]++;
sleep(1);
flag = -1;

}
}
}

}

pthread_mutex_unlock(&mutex);// mutex unlocking
}

int main(int argc, char **argv){

FILE *fp;
char *inputFile;
pthread_t *tid;
int *status;
int inputNumber, i, j, diff, searchstart, searchend;
int result = 0;
pthread_mutex_init(&mutex, NULL);


count = argc -3;
inputNumber = atoi(argv[1]);
inputFile = argv[2];

searchword = (char **)malloc(sizeof(char *)*count);
tid = malloc(sizeof(pthread_t)*inputNumber);
strlength = (int *)malloc(4*count);
status = (int *)malloc(4*inputNumber);
wordcount = (int *)malloc(4*count);

for(i = 0; i < count; i++)
searchword[i] = (char*)malloc(sizeof(char)*(strlen(argv[i+3]) + 1));
for(i = 3; i < argc; i++)
strcpy(searchword[i-3], argv[i]);

fp = fopen(inputFile, "r");
fseek(fp, 0, SEEK_END);
fsize = ftell(fp);
rewind(fp);
buffer = (char *)malloc(1*fsize);

fread(buffer, fsize, 1, fp);

diff = fsize / inputNumber;
if(diff == 0)
diff = 1;

for(i = 0; i < count ; i++){
strlength[i] = strlen(searchword[i]);
wordcount[i] = 0;
}


for(i = 0; i < inputNumber; i++){

searchstart = 0 + i*diff;
searchend = searchstart + diff;

if(searchstart > fsize)
searchstart = fsize;
if(searchend > fsize)
searchend = fsize;

if( i == inputNumber -1)
searchend = fsize;

params a;
a.num1 = searchstart;
a.num2 = searchend;

result = pthread_create(&tid[i], NULL, childfunc, (void *)&a);

if(result < 0){
perror("pthread_create()");
}

}

//스레드 받는 부분

for(i = 0; i < inputNumber; i++){
result = pthread_join(tid[i], (void **)status);

if(result < 0)
perror("pthread_join()");
}
pthread_mutex_destroy(&mutex); // mutex 해제
for(i = 0; i < count; i++)
printf("%s : %d \n", searchword[i], wordcount[i]);

free(searchword[i]);
free(searchword);

free(buffer);
free(tid);
free(strlength);
free(wordcount);
free(status);

fclose(fp);


return 0;
}

最佳答案

您的算法没有任何并行性,因为您在整个子进程周围使用了全局互斥体。此时很难看出您想做什么。一些建议:

  • 删除线程,首先进行有效的顺序编程。

  • 将数据分成独立的部分,以便您可以并行处理它,也可以按顺序处理它。

  • 现在添加线程并使其工作应该更容易一些。

关于c - 如何在C中使用线程和互斥体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34238292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com