具有独特的结构作为参数C PTHREAD [英] pthread with unique struct as parameter C

查看:122
本文介绍了具有独特的结构作为参数C PTHREAD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这块code,它是给我的麻烦。
我知道所有的线程读取相同的结构。但我不知道如何解决这个问题。

 的#include< pthreads.h中>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;typedef结构{
  诠释A,B;
} s_param;无效*
ThreadFunc中(无效* PARM)
{
  s_param *参数2 = PARM;
  的printf(ID:%d个和v数:%d \\ n,param2->一种,param2-> B);
  了pthread_exit(NULL);
}INT主(INT ARGC,字符** argv的)
{
  的pthread_t螺纹[3];
  INT RC = 0,I;
  void *的状态;  对于(I = 0;我3; ++ⅰ){
    s_param参数;
    param.b = 10;
    param.a = I;
    RC = pthread_create的(安培;螺纹[Ⅰ],NULL,ThreadFunc中,&放大器; PARAM); // !!!!
    如果(RC){
      出口(1);
    }
  }  对于(I = 0;我3; ++ⅰ){
    在pthread_join(线程[I],和放大器;状态);
  }
  返回0;
}

输出:

  ID:2和v:10
编号:2和v:10
编号:2和v:10

和我需要什么:

  ID:0和v:10
编号:1和v:10
编号:2和v:10


解决方案

在该sparam for循环的范围是循环内的静态。当您设置.A和.B你一遍又一遍地写相同的结构,并且所有三个线程获得一个指向同一个结构。

相反,您可以通过他们的数组,像这样创建结构的三个独立的实例。

  INT主(INT ARGC,字符** argv的)
{
  的pthread_t螺纹[3];
  s_param参数[3];
  INT RC = 0,I;
  void *的状态;  对于(I = 0;我3; ++ⅰ){
    参数[I] .B = 10;
    参数[I] .A = I;
    RC = pthread_create的(安培;螺纹[Ⅰ],NULL,ThreadFunc中,&放大器; PARAM [I]); // !!!!
    如果(RC){
      出口(1);
    }
  }
...等

应该提到,创造了一个这样的数组的结构(和线程),因为你明明联接()与主线程是唯一可行的。如果你没有这样做加盟,你会被告知要么静态分配结构的主要功能外,还是从堆的malloc他们,因为一旦进入线程退出的主要功能,包含数组堆栈帧将变得无效,不久将在一个未predictable方式来覆盖。

I have this piece of code that is giving me trouble. I know all the threads are reading the same struct. But I have no idea how to fix this.

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

typedef struct {
  int a,b;
} s_param;

void *
threadfunc(void *parm)
{
  s_param *param2 = parm; 
  printf("ID:%d and v:%d\n",param2->a,param2->b);
  pthread_exit(NULL);
}

int main(int argc, char **argv)
{
  pthread_t thread[3];
  int rc=0,i;
  void * status;

  for(i=0; i<3 ; ++i){
    s_param param;
    param.b=10;
    param.a=i;
    rc = pthread_create(&thread[i], NULL, threadfunc, &param ); // !!!!
    if(rc){
      exit(1);
    }
  }  

  for(i=0; i<3 ; ++i){
    pthread_join(thread[i],&status);
  }
  return 0;
}

output:

ID:2 and v:10
ID:2 and v:10
ID:2 and v:10

and what I need:

ID:0 and v:10
ID:1 and v:10
ID:2 and v:10

解决方案

The scope of the sparam in the for loop is static within that loop. When you set .a and .b you are writing to the same struct over and over, and all three threads are getting a pointer to that same single struct.

Instead, you could create three separate instances of the struct by making an array of them like so..

int main(int argc, char **argv)
{
  pthread_t thread[3];
  s_param param[3];
  int rc=0,i;
  void * status;

  for(i=0; i<3 ; ++i){
    param[i].b=10;
    param[i].a=i;
    rc = pthread_create(&thread[i], NULL, threadfunc, &param[i] ); // !!!!
    if(rc){
      exit(1);
    }
  } 
... etc

Should mention that creating the structs (and the threads) in an array like this is only feasible since you clearly do a join() with the main thread. If you didn't do that join, you would be advised to either statically allocate the structs outside of the main function, or malloc them from the heap, because as soon as the entry thread exits the main function, the stack frame containing the array will become invalid and will soon be overwritten in an unpredictable way.

这篇关于具有独特的结构作为参数C PTHREAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆