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

时间:2015-12-12 09:23:40

标签: c multithreading

我正在努力解决这个问题。它是关于从文本文件中计算某些模式的。我试图使用线程(分而治之)处理,但它返回错误的值。我使用互斥值来同步临界区。

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

代码低于

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;
}

1 个答案:

答案 0 :(得分:0)

您的算法没有任何并行性,因为您在整个孩子周围使用全局互斥。在这一点上,你很难看到你想要做什么。一些建议:

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

  • 将数据划分为独立的部分,以便您可以并行处理它,使其顺序工作。

  • 现在添加线程并使其正常工作会更容易。