A
anonymous
Hello Experts,
I was wondering about this problem how to solve:
struct async{
int b;
};
async_task(struct async *syn)
{
printf("%s %d\n", __func__, syn->b);
}
main()
{
int i;
struct async *syn = malloc(sizeof(*syn)*10);
for(i=0;i<10;i++) {
syn->b = &i;
async_task(syn) //this function will be called later as it is async
}
//free the memory and do other stuff
}
Problem is async_task is called 10 times rapidly and it is
possible that for loop gets over before async_task function
gets called. So when async_task gets called then it will have
the last value of "syn" i.e. the value gets overwritten as for loop
is executing and async_task function will get last value.
so printf is printing 9 all the times currently. I want it to print
1, 2,3,..... till 9. Basically i want to call async_func for all the values
i had given in argument.
c purists: i apologize for not giving the full code but i am after the
logic. I just want to see how you synchronize in this case.
Just give me a outline.
Solution: I think i can just make a struct and make "sun" as part
of the struct and pass it as below:
struct data{
struct async *syn1;
int counter;
};
struct async{
int b;
};
async_task(struct data *data)
{
for(i=0;i<data->counter;i++)
printf("%d\n", data->syn.b);
}
main()
{
int i;
struct data *data = malloc(sizeof(*data));
data->syn1 = malloc(sizeof(*syn)*10);
struct async *syn=malloc(sizeof(*syn));
for(i=0;i<10;i++) {
syn->b= &i;
data->syn = syn;
data.counter++;
}
async_task(data) ;
}
Is there any better way. Code above is not even compiled
as i am after the logic.
I was wondering about this problem how to solve:
struct async{
int b;
};
async_task(struct async *syn)
{
printf("%s %d\n", __func__, syn->b);
}
main()
{
int i;
struct async *syn = malloc(sizeof(*syn)*10);
for(i=0;i<10;i++) {
syn->b = &i;
async_task(syn) //this function will be called later as it is async
}
//free the memory and do other stuff
}
Problem is async_task is called 10 times rapidly and it is
possible that for loop gets over before async_task function
gets called. So when async_task gets called then it will have
the last value of "syn" i.e. the value gets overwritten as for loop
is executing and async_task function will get last value.
so printf is printing 9 all the times currently. I want it to print
1, 2,3,..... till 9. Basically i want to call async_func for all the values
i had given in argument.
c purists: i apologize for not giving the full code but i am after the
logic. I just want to see how you synchronize in this case.
Just give me a outline.
Solution: I think i can just make a struct and make "sun" as part
of the struct and pass it as below:
struct data{
struct async *syn1;
int counter;
};
struct async{
int b;
};
async_task(struct data *data)
{
for(i=0;i<data->counter;i++)
printf("%d\n", data->syn.b);
}
main()
{
int i;
struct data *data = malloc(sizeof(*data));
data->syn1 = malloc(sizeof(*syn)*10);
struct async *syn=malloc(sizeof(*syn));
for(i=0;i<10;i++) {
syn->b= &i;
data->syn = syn;
data.counter++;
}
async_task(data) ;
}
Is there any better way. Code above is not even compiled
as i am after the logic.