C pthread segmentation fault(内核转储)错误

C pthread segmentation fault(内核转储)错误,c,segmentation-fault,pthreads,C,Segmentation Fault,Pthreads,有人能指出我的代码为什么会出现分段错误吗?调用pthread\u create()时很可能出错,但我无法理解错误。有人能指出我哪里出了问题吗 #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> int x = 0; int counter = 0; // This is the global variable that all t

有人能指出我的代码为什么会出现分段错误吗?调用
pthread\u create()
时很可能出错,但我无法理解错误。有人能指出我哪里出了问题吗

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

int x = 0;
int counter =
    0;  // This is the global variable that all the threads will increment.
pthread_mutex_t lock;

void* addcounter(void* threadid) {
  pthread_mutex_lock(&lock);
  int* id = (int*)threadid + 1;
  int* y = (int*)x;
  int total = (*id) * (*y);
  int i = 0;
  for (i = 0; i < total; i++) {
    counter += 1;
  }
  pthread_mutex_unlock(&lock);
}

int main(int argc, char* argv[]) {
  int thread_count = 0;
  int loop_count = 0;
  int expected_value = -1;

  if (argc != 3) {
    printf("Usage: threads <no_of_threads> <no_of_loops>\n");
    exit(1);
  }

  thread_count = atoi(argv[1]);
  loop_count = atoi(argv[2]);
  x = loop_count;

  // Start your implementation after this comment.
  pthread_t* threads;
  threads = malloc(sizeof(pthread_t) * thread_count);
  int t;
  int flag = 0;
  for (t = 0; t < thread_count; t++) {
    int* z = malloc(sizeof(int));
    *z = t;
    flag = pthread_create(&threads[t], NULL, addcounter, (void*)z);
  }
  if (flag != 0) {
    printf("error in creating threads");
    exit(0);
  }

  for (t = 0; t < thread_count; t++) pthread_join(threads[t], NULL);
  // Do not change the code below this comment
  return 0;
}
#包括
#包括
#包括
#包括
int x=0;
整数计数器=
0;  // 这是所有线程将递增的全局变量。
pthread_mutex_t lock;
void*addcounter(void*threadid){
pthread_mutex_lock(&lock);
int*id=(int*)threadid+1;
int*y=(int*)x;
整数总计=(*id)*(*y);
int i=0;
对于(i=0;i
此代码的输出为:

分段故障(堆芯转储)


如何解决此错误?

首先,您应该使用
PTHREAD\u mutex\u初始值设定项来初始化
mutex
变量

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
或者您可以从主线程调用
pthread\u mutex\u init(&lock,0)
来初始化
lock

第二件事,替换下面的语句

 int* y = (int*) x;

因为
y
应该有
有效地址
,否则您以后不能执行
*y
(您正在执行的操作)


我希望它能有所帮助。

中的多指针问题

void* addcounter(void* threadid) { 
  pthread_mutex_lock(&lock);
  int* id = (int*)threadid + 1;    // <== A
  int* y = (int*)x;                // <== B
  int total = (*id) * (*y);        // <== C
  int i = 0;
  for (i = 0; i < total; i++) {
    counter += 1;
  }
  pthread_mutex_unlock(&lock);
}
void*addcounter(void*threadid){
pthread_mutex_lock(&lock);

int*id=(int*)threadid+1;//是的,这是什么?
int*y=(int*)x;
x
是一个整数,不是指针。用-g编译,用gdb运行,它会告诉你crashingOhh Bo Persson在哪里得到的it@BoPersson我应该更改什么?”
(int*)threadid+1;
“为什么要更改
+1
?@ShahzaibRahim现在检查一下。
void* addcounter(void* threadid) { 
  pthread_mutex_lock(&lock);
  int* id = (int*)threadid + 1;    // <== A
  int* y = (int*)x;                // <== B
  int total = (*id) * (*y);        // <== C
  int i = 0;
  for (i = 0; i < total; i++) {
    counter += 1;
  }
  pthread_mutex_unlock(&lock);
}