C中的pthreads:如何阻止线程相互干扰?

我是使用 pthreads 的新手。我想创建一个程序,其中六个不同的线程将分别输出不同的数字。线程可以以任何顺序运行,但是,每个线程只能运行一次。

因此,可能的输出是:

Thread: 5
Thread: 2
Thread: 3
Thread: 6
Thread: 1
Thread: 4

或者它可以是任何其他顺序。

#include<stdio.h>
#include<pthread.h>
    
void *apples(void *void_apple_num){
      int *thread_num = (int *) void_apple_num;
      printf("Thread: %dn", *thread_num);
      return NULL;
}



int main(){
  pthread_t threads[6];
  int apple_num;
  

  for(apple_num=0; apple_num<6; apple_num++){
       pthread_create(&threads[apple_num], NULL, apples, &apple_num);
  }

  for(apple_num=0; apple_num<6; apple_num++){
       pthread_join(threads[apple_num], NULL);
  }    

  return 0;
}

当我运行程序时,我遇到了线程相互干扰的问题。我不确定某些线程是否运行两次?但是,我认为问题在于某些线程正在使用apple_num来自不同线程指针。

这是我得到的两个示例输出:

输出 1:

Thread: 5
Thread: 0
Thread: 1
Thread: 1
Thread: 2
Thread: 2

输出 2:

Thread: 1
Thread: 4
Thread: 4
Thread: 5
Thread: 1
Thread: 1

我不完全明白是什么导致了问题。我听说线​​程有时可以共享变量?我不知道我是否应该使用互斥锁以某种方式让线程一次运行一个。

如何编辑我的代码来解决这个问题?

如果其他人问过类似的问题,请引导我回答他们的问题。我在研究时找不到任何东西。

回答

您的每个线程都会获得一个指向apple_num主线程在循环中更改的相同局部变量的指针。由于线程是异步启动的,apple_num从任何其他线程的角度来看,主线程中局部变量的值是不确定的。

您需要将该变量的副本传递给每个线程。

一种解决方法是投射intvoid*和返回:

void *apples(void* apple_num){
    int thread_num = (int)void_apple_num;
...
    pthread_create(&threads[apple_num], NULL, apples, (void*)apple_num);

正如他们在评论中提到的,intptr_tand uintptr_t(from <stdint.h>) 可能更适合无​​损失的往返,例如uintptr_t-> void*-> uintptr_t。但是 C 标准不需要任何整数来回往返void*,它只需要void*->intptr_t和返回。

在更现实的场景中,您可能希望将多个整数传递给线程,即 a struct。这就是线程启动函数接收单个void*参数的基本原理——它可以指向任何数据类型的对象(PO​​SIX 需要void*还要求能够存储函数指针)。

将结构传递给线程的示例(不依赖于实现定义的整数void*来回转换):

struct ThreadArgs {
    int thread_num;
    // More data members, as needed.
};

void* apples(void* arg){
    struct ThreadArgs* a = arg;
    printf("Thread: %dn", a->thread_num);
    free(arg);
    return NULL;
}

int main() {
    pthread_t threads[6];
    struct ThreadArgs* a;
    int apple_num;

    for(apple_num=0; apple_num<6; apple_num++){
        a = malloc(sizeof *a);
        a->thread_num = apple_num;
        pthread_create(&threads[apple_num], NULL, apples, a);
    }
    
    for(apple_num=0; apple_num<6; apple_num++)
        pthread_join(threads[apple_num], NULL);
    
    return 0;
}

请注意,您不必在堆 ( malloc)上分配线程参数结构。如果将自动变量(在堆栈上)传递给线程,则必须确保该变量未更改并且在线程访问它时仍然存在。从堆分配线程参数结构是最安全的,并且以malloc/free调用为代价解决了这个问题。

  • If you store an integer as a pointer, us `uintptr_t` or `intptr_t`.
  • It depends on what your compilation options are and your tolerance for compiler warnings. For my purposes, I have zero tolerance for compiler warnings, and converting an `int` to pointer generates a warning/error "cast to pointer from integer of different size [-Werror=int-to-pointer-cast]" — so I would use `uintptr_t` as suggested.

以上是C中的pthreads:如何阻止线程相互干扰?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>