首页 > 解决方案 > pthread_create 跳过或重复

问题描述

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

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

线程 1 必须从 1 计算到 10

线程 2 必须从 11 计算到 20

...很快。

每个线程必须将其单独的总和返回到一个用零初始化的全局变量总和。

我的尝试:

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

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

我得到的输出是:

输出画面

我的问题是为什么它会重复一些值并跳过一些值?我没有正确使用 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);

}

标签: cpthreadsposix

解决方案


此代码将相同的地址传递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允许转换回的特定平台行为的存在int,因此它不是严格兼容的 C 代码:

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;

这将作为指针值传递ivoid *

但如果intptr_t存在,这会更好:

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

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

实际上,已经没有多少平台可以使用这种方法了。

或者,在严格符合 C 的情况下,要传递实际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]));
}

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

推荐阅读