C pthread 和 struct 问题

发布于 2024-11-04 04:19:05 字数 3003 浏览 0 评论 0原文

该代码的基本功能是获取计数器和线程的数量,创建计数器然后创建线程,然后获取每个线程中的指令数量(指令格式 [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;
}

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

Basic function of this code is to get number of counter and thread, creates the counters then create the threads then get numbers of instructions in each thread (format of instruction [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);

How do I actually store this three variable using 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;
}

Is the pthread_create function correct?

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

I also tried to change the ninstruction into array, if its correct...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅此而已 2024-11-11 04:19:05

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

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

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

OK, next attempt. This time in pseudo code since this smells like homework...

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

For protected sections, look up mutex or semaphore.

七颜 2024-11-11 04:19:05

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

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

我希望这就是您想要的......

Mario

OK. I assume you want to do the following:

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

And in the worker thread

worker_thread( void* arg )
{
   struct instruction* pInstruction = (struct instruction*)arg;

   pInstruction->repetions = 42;
...

I hope this is what you wanted...

Mario

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文