C pthread和struct问题
问题描述:
此代码的基本功能是获取计数器和线程数,创建计数器然后创建线程,然后获取每个线程中的指令数(指令格式[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变成数组,如果它的正确...
答
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
答
确定。我想你要做到以下几点:
...
//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;
...
我希望这是你想要的东西......
马里奥
你想在哪里存储什么?您必须以某种方式将正确的结构传递给线程(然后照顾同步化) – 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