此代碼的基本功能是獲取計數器和線程數,創建計數器然後創建線程,然後獲取每個線程中的指令數(指令格式[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變成數組,如果它的正確...
你想在哪裏存儲什麼?您必須以某種方式將正確的結構傳遞給線程(然後照顧同步化) – 2011-04-27 18:12:10
我想通過使用struct指令**指令格式將指令存儲到struct指令中。說輸入是1 D 10,我想要一個指令結構存儲爲計數器= 1,work_fn =增量,reptitions = 10. – Jono 2011-04-27 18:25:55
好吧,訪問結構指令** ppData將(* ppData) - >重複。 – 2011-04-27 18:53:25