【问题标题】:C pthread and struct questionsC pthread 和 struct 问题
【发布时间】:2011-04-27 18:03:53
【问题描述】:

此代码的基本功能是获取计数器和线程数,创建计数器然后创建线程然后获取每个线程中的指令数(指令格式 [counter] [work-function] [repetition])

/* ============================================================================
 * File-global variables
 * ========================================================================== */
static int ncounters = 0;
static struct counter *counters = NULL;

static int nthreads = 0;
static int *ninstructions = NULL;
static struct instruction **instructions = NULL;

struct counter {
   long long counter;            /* to store counter */
};

/* counter value */
struct instruction {
   struct counter *counter;      /* pointer to counter */
   int repetitions;              /* number of repetitions  */
   void (*work_fn)(long long *); /* function pointer to work function */
};

/* ============================================================================
 * Thread function
 * ========================================================================== */
static void *
worker_thread(void *arg) {
    (void)arg;
        int Tcounter;
        int Trepetition;
        char Tfuntion;

        scanf(" %d %c %d", &Tcounter, &Tfuntion, &Trepetition);

我如何使用struct指令**指令来存储这三个变量???

    return NULL;
}


/* ============================================================================
 * Main function
 * ========================================================================== */
int
main(void) {

        scanf(" %d", &ncounters);

        int i;

        if(counters = malloc(sizeof(struct counter)*ncounters)){
          for( i=0; i < ncounters ;i++){
            counters[i].counter = 0;
          }

          for( i=0; i < ncounters ;i++){
            printf("%lld\n", counters[i].counter);
          }
        }

        scanf(" %d", &nthreads);
        pthread_t threads[nthreads];

        int ninst;

        for( i=0; i < nthreads ;i++){
          scanf(" %d", &ninst);
          ninstructions = ninst;
          for( i=0; i < ninstructions ;i++){
            pthread_create(&threads[i], NULL, worker_thread, NULL);
          }
        }

        free(counters);
    return 0;
}

pthread_create 函数是否正确?

int
main(void) {

        scanf(" %d", &ncounters);

        int i;

        if(counters = malloc(sizeof(struct counter)*ncounters)){
          for( i=0; i < ncounters ;i++){
            counters[i].counter = 0;
          }
        }

        scanf(" %d", &nthreads);

        pthread_t threads[nthreads];

        if(ninstructions = (int*)malloc(nthreads*sizeof(int)){
          for( i=0; i < nthreads ;i++){
            scanf(" %d", &ninstructions[i]);
            int j;
            for(j=0; i < ninstructions[i] ;j++){
              pthread_create(&threads[j], NULL, worker_thread, NULL);
            }
          }
        }


        free(ninstructions);
        free(counters);
    return 0;
}

我也试过把指令改成数组,如果正确的话……

【问题讨论】:

  • 你想在哪里存储什么?你必须以某种方式将正确的结构传递给线程(然后处理同步)
  • 我想使用struct指令**指令格式将指令存储到struct指令中。说输入是 1 D 10,我想要一个指令结构存储为 counter = 1 , work_fn = Increment , reptitions = 10。
  • 好吧,访问结构指令** ppData 将是 (*ppData)->repetitions 。

标签: c struct pthreads


【解决方案1】:

好的,下次尝试。这次是伪代码,因为这闻起来像家庭作业......

struct instruction
{
   long long counter;
   int repetitions
};


main()
{
  ask #threads

  pthread_t threads[nthreads];
  struct instruction intructions[nthreads];

  while( i < nthreads )
  {
     pthread_create( &threads[i], NULL, threadMain, &instructions[i] );
  }

  i = 0
  while ( i < nthreads )
  {
    //now wait until all threads have finished
     pthread_join( threads[i], NULL );
  }
}

  void threadMain( void* arg )
  {
    struct instruction* pInstruction = (struct instruction*)arg;
    char cFunctionCode;

    enter protected section, otherwise all threads will ask at the same time

     scanf(" %d %c %d", &pInstruction->counter, &cFunctionCode, &pInstruction->repetitions 

   leave protected section here

   do youtr computes here

   return;
)

对于受保护的部分,查找互斥锁或信号量。

【讨论】:

    【解决方案2】:

    好的。我假设您想要执行以下操作:

    ...
    //create the instructions, which is missing.
    // be carefull that instruction.counter is not allocated!!!
    
    struct instruction instructions[ncounters];
    ... 
    pthread_create( &threads[i], NULL, worker_thread, &instructions[i] );
    ...
    

    在工作线程中

    worker_thread( void* arg )
    {
       struct instruction* pInstruction = (struct instruction*)arg;
    
       pInstruction->repetions = 42;
    ...
    

    我希望这是你想要的......

    马里奥

    【讨论】:

    • whoops, int *nstructions 将是一个动态分配的大小为 nthreads 的数组,其中包含每个线程的序列长度。
    • 我不太明白 = (struct instruction*)arg;有点,对不起。
    • 您的帖子中缺少的内容 ;-) 。其余的保持不变,将其作为参数(pthread_create 的第四个参数)传递给 worker_thread,您可以在主线程中访问它。
    • worker_thread 接收一个 void* 作为参数。您必须将其转换回正确的类型,以便您可以访问您的结构指令
    • 嗯,那么什么时候应该创建指令?在主线程或工作线程中?并且不应该将指令创建为数组,因为每个“唯一”线程都有“一个或多个”指令。我必须使用**,但我不太擅长** =[
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多