va_lists and threads

F

Felix Kater

The function f gets variable arguments and creates several threads
which each shall receive all of the variable arguments. Like so:

void f(
my_callback* array_of_callbacks,
...){

va_list va;
va_start(va,array_of_callbacks);

/* now loop through array */

/* create thread for each callback
and pass va for each callback
*/

/* end of loop */

va_end va;

}

(1) Is it ok to use the locally created va and return from f() even
though the threads may still run and access va?

(2) Is it ok to va_start va *only once* for all callbacks/threads in the
loop or do I have to provide copies like va1, va2, va3 for each
iteration? This would make thinks complicated since the count is
variable: I would have to dynamically create an array of va -- who
would finalize them, however?

Felix
 
M

mark_bluemel

Felix said:
The function f gets variable arguments and creates several threads

You'll probably get plenty of pointers that you are off-topic here.
The C language does not consider the possibility of multiple threads.

However, you are almost certainly looking to abuse the variable
arguments functionality. Read the language/library specification for
C89 (draft available free in pdf format online) and you see
stipulations such as "each invocation of va_start or va_arg shall be
matched by a corresponding invocation of va_end in the same function".
You also see that va_arg is defined as modifying the va_list object, so
sharing this is clearly unsafe.

Why not tell us a little more about what you are trying to do, and
we'll see whether we can help you achieve it in a robust manner, or
possibly we'll point you at a threads programming newsgroup.
 
A

aegis

Felix said:
The function f gets variable arguments and creates several threads
which each shall receive all of the variable arguments. Like so:

void f(
my_callback* array_of_callbacks,
...){

va_list va;
va_start(va,array_of_callbacks);

/* now loop through array */

/* create thread for each callback
and pass va for each callback
*/

/* end of loop */

va_end va;

}

(1) Is it ok to use the locally created va and return from f() even
though the threads may still run and access va?

(2) Is it ok to va_start va *only once* for all callbacks/threads in the
loop or do I have to provide copies like va1, va2, va3 for each
iteration? This would make thinks complicated since the count is
variable: I would have to dynamically create an array of va -- who
would finalize them, however?


I think using a separate va_list object
for each callback would be fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,123
Messages
2,570,718
Members
47,284
Latest member
LeroyOlver

Latest Threads

Top