2011-04-27 66 views
0

此代碼的基本功能是獲取計數器和線程數,創建計數器然後創建線程,然後獲取每個線程中的指令數(指令格式[counter] [work-function] [重複])C pthread和struct問題

/* ============================================================================ 
* 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; 
} 

我也試圖在ninstruction變成數組,如果它的正確...

+0

你想在哪裏存儲什麼?您必須以某種方式將正確的結構傳遞給線程(然後照顧同步化) – 2011-04-27 18:12:10

+0

我想通過使用struct指令**指令格式將指令存儲到struct指令中。說輸入是1 D 10,我想要一個指令結構存儲爲計數器= 1,work_fn =增量,reptitions = 10. – Jono 2011-04-27 18:25:55

+0

好吧,訪問結構指令** ppData將(* ppData) - >重複。 – 2011-04-27 18:53:25

回答

2

OK,下一個嘗試。這一次在僞代碼,因爲這味道像功課...

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

對於受保護的部分,查找互斥體或信號量。

+0

非常感謝馬里奧 – Jono 2011-04-27 20:23:27

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

我希望這是你想要的東西......

馬里奧

+0

whoops,int * ninstructions將是一個動態分配的包含每個線程序列長度的nthread數組。 – Jono 2011-04-27 19:10:22

+0

我不太明白=(struct instruction *)arg;位,對不起。 – Jono 2011-04-27 19:11:14

+0

您的帖子中缺少;-)。其餘的保持不變,將它作爲參數(pthread_create的第四個參數)傳遞給worker_thread,並且您可以在thread main中訪問它。 – 2011-04-27 19:12:34