小编Dan*_*ida的帖子

在C中使用pthread_join时出现分段错误

所以,我正在做一些我的家庭作业而且我被困在这个段上.我试图调用pthread_join时遇到的错误.

我尝试了不同的解决方案,包括创建一个void指针来发送到pthread_join调用.

这是我的代码:

#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <semaphore.h>
#include <time.h>
#include <pthread.h>

#define NTHREADS 5
#define NVALORES 1000
#define NUMBER_PROCURADO 890

void * run(void *arg);
void fillVector();
int vetor[NVALORES];

int main(){

    int i, vetor2[NTHREADS];
    pthread_t threads[NTHREADS];
    fillVector();
    for (i = 0; i < NTHREADS; i++){
        vetor2[i]=i;
        threads[i]= pthread_create(&threads[i], NULL, run, &vetor2[i]);
    }

    for (i = 0; i < NTHREADS; i++){
        pthread_join(threads[i], NULL);
    } …
Run Code Online (Sandbox Code Playgroud)

c pointers pthreads segmentation-fault

2
推荐指数
1
解决办法
81
查看次数

标签 统计

c ×1

pointers ×1

pthreads ×1

segmentation-fault ×1