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