pthread_create跳过或重复

时间:2018-05-03 09:39:26

标签: c pthreads posix

pthread_create函数被跳过或有时被调用两次。 我要解决的问题是:

给定一个包含1到100之间数字的全局数组。您需要创建10个线程,每个线程必须找到10个数字的平方和。

线程1必须从1到10

计算

线程2必须从11到20计算

......等等。

每个线程必须将其各自的和返回到用零初始化的全局变量sum。

我的尝试:

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<stdlib.h>
#include<semaphore.h>

int arr[100];

sem_t s;

int sum=0;

void *calculate(void *i){
    sem_wait(&s);
    int j=(*((int*)i));
    int k;
    printf("j: %d\n",j);
    int temp=0;
    for(k=j*10;k<(j*10)+10;k++){
        temp=temp+(arr[k]*arr[k]);
    }
    sum+=temp;

    printf("sum: %d j: %d\n\n",sum,j);

    sem_post(&s);
    pthread_exit(NULL);
}

int main(){
    sem_init(&s,0,1);
    pthread_t threads_array[10];
    int i=0;
    int *k=(int *)malloc(sizeof(int));
    for(i=0;i<100;i++){
        arr[i]=i+1;
    }

    int temp=0,temp_i;

    for(i=0;i<10;i++){
        (*k)=i;
        printf("k: %d\n",(*k));
        pthread_create(&(threads_array[i]),NULL,calculate,(void*)k);
    }
    for(i=0;i<10;i++){
       pthread_join(threads_array[i],NULL);
    }

    printf("%d",sum);
    return 0;
}

我使用过信号量。这样一次只有一个线程可以访问全局资源。

我得到的输出是:

Output screen

我的问题是为什么重复一些价值并跳过一些?我没有正确使用pthread_create?

我每次都尝试使用k的新值:

for(i=0;i<2;i++){
    int *k=&i;
    printf("k: %d\n",(*k));
    pthread_create(&(threads_array[i]),NULL,calculate,(void*)k);

}

1 个答案:

答案 0 :(得分:0)

此代码将k的相同地址传递给每个线程,但会更改该内存中的值

for(i=0;i<10;i++){
    (*k)=i;
    printf("k: %d\n",(*k));
    pthread_create(&(threads_array[i]),NULL,calculate,(void*)k);
}

此代码运行时

void *calculate(void *i){
    sem_wait(&s);
    int j=(*((int*)i));
        .
        .
        .

该值可能已更改,因为主线程已更改它。

这会更好,因为它传递i,但它取决于intptr_t的存在以及特定于平台的行为,允许转换回{ {1}},所以它不是严格遵守C代码:

int

for(i=0;i<10;i++){
    pthread_create(&(threads_array[i]),NULL,calculate,(void*)(intptr_t)i);
}

void *calculate(void *i){ sem_wait(&s); int j= (intptr_t)i; 作为i指针值传递。

但如果void *存在,那就更好了:

intptr_t

intptr_t i;
    .
    .
    .
for(i=0;i<10;i++){
    pthread_create(&(threads_array[i]),NULL,calculate,(void*)i);
}

实际上,目前还没有很多平台可以解决这个问题。

或者,在严格符合C的情况下,为了传递实际void *calculate(void *i){ sem_wait(&s); intptr_t j= (intptr_t)i; 值的地址,对于线程运行时保证存在的每个线程,您需要单独的int

int

// this is a local variable, but since it's in the same scope as
// both pthread_create() and pthread_join(), it will still exist
// for the entire lifetime of each thread created
int threadnum[10];

for(i=0;i<10;i++){
    threadnum[i]=i;
    pthread_create(&(threads_array[i]),NULL,calculate,(void*)&(threadnum[i]));
}