C pthread and struct questions
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;
}
/* ========================开发者_StackOverflow社区====================================================
* 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...
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.
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
精彩评论