使用线程时出现分段错误 - c

问题描述 投票:0回答:4

这是我第一次使用线程,我从一个简单的程序开始。该程序接受

n
参数并创建
n-2
线程。问题是我遇到了分段错误,但我不知道为什么。

代码如下:

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

void *
removeBytes (int i, char* argv[])
{
  printf ("%d, %s\n", i, argv[i]);
  return NULL;
}  


int main (int argc, char *argv[])
{

  pthread_t threads[argc - 3];
  int err;
  int i;  
  int *ptr[argc - 3];

  printf ("argc = %d\n", argc);

  for (i = 0; i < argc -3; i++)
    {
      err =
        pthread_create (&(threads[i]), NULL,
                        removeBytes(i+1,&argv[i+1]), NULL);
      if (err != 0)
        {
          printf ("\nCan't create thread: [%d]", i);
        }
      else
        { 
          printf ("\nThread created successfully\n");
        }
    }

  for (i = 0; i < argc - 3; i++)
    {
      pthread_join (threads[i], (void **) &(ptr[i]));
      printf("pthread_join - thread %d",i);
    }

  return 0;
}

示例:我的程序称为

mythread
所以当我运行它时
./mythread f1 f2 f3 f4 f5 f6
输出为:

argc = 6

1,f2
Thread created successfully

2,f4
Thread created successfully
3, (null)

为什么把

f2
当作
argv[1]
f4
当作
argv[2]

更新:

 typedef struct{
    int i;
    char* argv;
  }Data;

  void* removeBytes(void* arg){
    Data* data = (Data*)arg;   
    printf("%d, %s\n",data->i, data->argv);
    free(data);
    return NULL;
  }



  int main(int argc, char** argv){
    Data* data;

    pthread_t threads[argc-3];

    int i;
    int err;
    for(i=0; i < argc-3;i++){
      data = (Data*)malloc(sizeof(Data));
      data->i=i+1;
      data->argv=argv[i+1];
      err = pthread_create(&(threads[i]),NULL,removeBytes,data);
      if(err != 0){
        printf("\nCan't create thread %d",i);
      }
      else{
        printf("Thread created successfully\n");
      }
    }  

    return 0;
  }

对于 ./mythread f1 f2 f3 f4 f5 f6 f7 f8 输出是:

5 x“线程创建成功”。它不打印 i 或 argvi[i]。

c pthreads
4个回答
0
投票

有问题

pthread_create (&(threads[i]), NULL,
                    removeBytes(i+1,&argv[i+1]), NULL);

pthread_create
的语法是

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                      void *(*start_routine) (void *), void *arg);

它将启动例程作为回调。在您的情况下,您在生成并返回 NULL 之前在主线程中调用removeBytes。所以,回调为 NULL。

因此,根据您的需要相应地修改您的removeBytes并调用

pthread_create(&(线程[i]), NULL, 删除字节,argv[i+1]);


0
投票

您没有正确使用

pthread_create
:

pthread_create (&(threads[i]), NULL,
                        removeBytes(i+1,&argv[i+1]), NULL);

在这里,您只需调用

removeBytes()
并将结果 (
NULL
) 作为
pthread_create()
的参数传递。

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

第三个参数应该是指向

void* myThread(void*)
函数的指针。如果要将参数传递给线程,则应使用
void*
参数并将其作为
pthread_create
的第三个参数传递。

您可以查看

this
以了解如何使用 pthread 库。

另外,您可能想要这样的东西:

typedef struct {
    int i;
    char* argv;
} Data;

void * removeBytes (void* arg)
{
  Data* data = (Data*) arg;
  printf ("%d, %s\n", data->i, data->argv);
  free(data);
  return NULL;
}

然后像这样创建线程:

Data* data = (Data*)malloc(sizeof(Data));
data->i = i;
data->argv = argv[i+1];
err = pthread_create (&(threads[i]), NULL, removeBytes, data);

0
投票

它将 f2 作为 argv[1],将 f4 作为 argv[2] 只是因为当您传递 &argv[i+1] 时,您实际上传递了指向数组的第 (i+1) 个元素的指针,并且还传递了 i+ 1 作为索引。

因此,如果 argv 等于 [mythread, f1, f2, f3, f4, f5, f6],首先您将在removeBytes 中得到 [f1, f2, f3, f4, f5, f6]。当您访问 i+1 = 1 元素时,您将得到 f2。下次你会得到 [f2, f3, f4, f5, f6] 和 i+1 = 2 所以你会得到 f4


0
投票

pthread_create (&(threads[i]), NULL,
                    removeBytes(i+1,&argv[i+1]), NULL);

您正在调用

removeBytes()
,而不是将其作为参数传递。

此外,您只能将一个参数传递给线程函数。因此,您需要将多个参数放入一个结构中。比如:

struct thread_args {
    int i;
    char *argv;
}

#your main code
struct thread_args *thargs;

for (i = 0; i < argc -3; i++)
{
  thargs = malloc(sizeof(*thargs));
  thargs->i = i+1;
  thargs->argv = argv[i+1];
  err =
    pthread_create (&(threads[i]), NULL,
                    removeBytes, thargs);
  if (err != 0)
    {
      printf ("\nCan't create thread: [%d]", i);
    }
  else
    { 
      printf ("\nThread created successfully\n");
    }
}
#make sure to free up thargs as well.

并将线程函数更新为

void *removeBytes (void *arg)
{
  int i;
  char *argv;
  struct thread_args *thargs =  (struct thread_args *) arg;
  i = thargs->i;
  argv = thargs->argv;
  printf ("%d, %s\n", i, argv);
  return NULL;
} 
© www.soinside.com 2019 - 2024. All rights reserved.