C pthread和struct问题

时间:2011-04-27 18:03:54

标签: c struct pthreads

此代码的基本功能是获取计数器和线程的数量,创建计数器然后创建线程然后获取每个线程中的指令数量(指令格式[计数器] [工作函数] [重复])

/* ============================================================================
 * 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 instruction ** instructions ???

实际存储这三个变量
    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;
}

我还尝试将ninstruction更改为数组,如果正确的话......

2 个答案:

答案 0 :(得分:2)

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

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;
)

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

答案 1 :(得分:0)

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

...
//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;
...

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

马里奥