gpt4 book ai didi

C - 多线程 WordCount 运行时崩溃 - 现在编译失败 : redefinition of struct timespec?

转载 作者:行者123 更新时间:2023-11-30 20:23:33 24 4
gpt4 key购买 nike

我正在为类(class)开发一个多线程字数统计程序,我已经完成了大部分工作,但是,尽管使用警告标签成功编译了,但我的程序立即崩溃了。因此,基本上该程序应该获取给定的 txt 文档(这只是书中的文本)并将其分成 16 个线程,这些线程分别对单词进行计数,并将总数在 main 中求和。这是我的代码:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

pthread_mutex_t mutex;
//pthread_cond_t cond;


typedef struct thread_data
{
int input;
int offset;
int size;
int wordCount;
} thread_data;

void *counter(void *arg)
{
thread_data *data = (thread_data *)arg;
char *buffer = malloc(data->size);
int i;
int count = 0;
int isChar = 0;

pthread_mutex_lock(&mutex);

lseek(data->input, data->offset, SEEK_SET);
read(data->input, buffer, data->size);

for (i = 0; i < data->size; i++)
{
if (buffer[i] == ' ' || buffer[i] == '\t' || buffer[i] =='\n')
{
isChar = 0;
}
else
{
if (isChar == 0)
{
count++;
}
isChar = 1;
}
}
data->wordCount = count;
free(buffer);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);

return NULL;
}

int main()
{
int id;
int segSize;
int total;
//int fileSize;
pthread_t *threads;
int input;

input = open("Assign3.txt", O_RDONLY); //open file

segSize = lseek(input,0,SEEK_SET)/16; //Getting segment size for partition

struct thread_data data[16];

threads = malloc(16*sizeof(pthread_t));

for(id =0; id < 16; id++) //Partitioning of file to threads
{
data[id].input = input;
data[id].offset = id*segSize;
}

pthread_mutex_init(&mutex, 0);

for(id = 0; id < 16; id++) //create threads
{
pthread_create(&threads[id], NULL, &counter, &data[id]);
}
for(id = 0; id < 16; id++)
{
pthread_join(threads[id], NULL);
}

for(id = 0; id < 16; id++) //compute total from threads
{
total += data[id].wordCount;
}

printf("There are %i words in this document.\n", total);

//cleanup
pthread_mutex_destroy(&mutex);
close(input);

return 0;
}

以前,这会编译并且在运行时会崩溃。我不认为我改变了任何东西,但是现在当我使用 MinGW 和命令编译它时(第一个正是我的教授将如何编译代码)

gcc -Wall -Werror -lpthread Assign3_MichaelMorro.c
//or
gcc -Wall -Werror Assign3_MichaelMorro.c

我收到以下错误:

In file included from Assign3_MichaelMorro.c:3:0:
c:\mingw\include\pthread.h:320:8: error: redefinition of 'struct timespec'
struct timespec {
^
In file included from c:\mingw\include\time.h:53:0,
from c:\mingw\include\pthread.h:219,
from Assign3_MichaelMorro.c:3:
c:\mingw\include\parts\time.h:105:8: note: originally defined here
struct timespec
^

我该如何处理这个问题?在带有 native gcc 的 Linux 计算机上执行此操作是否可能有效?如果有人使用 Linux 并且可以尝试为我编译这个,我将不胜感激。

最佳答案

当我编译你的代码时,我收到警告。对我来说,这些警告似乎相当严重。

$  gcc -Wall a.c -o a -lpthread
a.c: In function 'counter':
a.c:28: warning: cast from pointer to integer of different size
a.c:29: warning: cast from pointer to integer of different size
a.c: In function 'main':
a.c:65: warning: cast from pointer to integer of different size

当我查看这些行时,我发现您正在将 FILE* 转换为 int

这完全是错误的。

您应该将 lseek 函数调用替换为适用于 FILE* 的函数调用,例如 fseek

或者,如果您更喜欢使用 lseek/read 等,您应该使用 open 打开文件,它返回一个文件描述符(类型 int)。

如果由于某种原因,您不想切换到 open(),您可以从 FILE* fp 获取文件描述符,如下所示:

int file = fileno(fp);

关于C - 多线程 WordCount 运行时崩溃 - 现在编译失败 : redefinition of struct timespec?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35936924/

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