2

I want to print elements of array using single printf. The array is small (for example up to 10 elements), but each time I may have to print different number of first elements of array.

I know I can do this:

count = how_many_first_elements_i_need_this_time();
printf("array elements: ");
for (i = 0; i < count; ++i)
    printf("%d ", array[i]);
printf("\n");

But this may lead to interleaving outputs of several printf executed at the same time by different threads, I suppose.

I can write huge switch to handle all possible cases, but it is ugly.

Any other suggestions?

5
  • 1
    You can concat your output and display it at the end Commented Jul 4, 2013 at 9:45
  • 3
    "But this may lead to interleaving outputs of several printf executed at the same time by different threads, I suppose." - yes, and it wouldn't be different if you used only one call. Thread management disregards function call boundaries. Commented Jul 4, 2013 at 9:51
  • 1
    If the purpose is very simple, like for logging, then you could print thread-id with array element to track Commented Jul 4, 2013 at 9:52
  • 1
    I agree with H2CO3: In a multithreaded environment, you need to protect the output file (here: stdout) against concurrent access, even if you'd print out all array's membere using one call to printf(). Commented Jul 4, 2013 at 9:58
  • Thanks for clarification. But to be honest, I'm not using printf but some kind of linux kernel printer which wraps the phrase I'm providing with some additional data: pid, time, etc., so I BELIEVE it does synchronization. This is the reason I demand single printf call. I have used printf to make this question as simple as possible and to be useful for broader range of cases than mine. Commented Jul 4, 2013 at 10:20

3 Answers 3

8

One of the solution is use sprintf

Collect all the output in one char buffer and print the whole string only once.

You can allocate the buffer dynamically...

char *pcBuf;
int iLen = 0;
count = how_many_initial_elements_i_need_this_time();
printf("array elements: ");

pcBuf = malloc((count * MAX_SIZE_OF_NUM) + count +1); //+count is required for space, and 1 is required for NULL

for (i = 0; i < count; ++i)
    iLen += sprintf(pcBuf+iLen, "%d ", array[i]);

printf("%s \n", pcBuf);
Sign up to request clarification or add additional context in comments.

Comments

2

There is no formatting specifier to printf() to make it print multiple elements of an array automatically (except %s, but that's stretching it).

So, you're going to need to keep the loop.

To make each thread's output more atomic (I'm not sure it's guaranteed that each printf() is truly atomic) you can build the output in a string buffer which is then printed all at once.

One way of doing that is by using sprintf() in a loop:

char out[1024], *put = out;

for(i =0; i < count; ++i)
  put += sprintf(put, "%d ", array[i]);
strcat(put, "\n"); /* Fast! */
printf(out);

Of course, this can be adjusted to not buffer overflow if there's a risk, but 10 integers in 1,024 characters should be pretty safe.

Comments

1

You may use some type of thread sync (if you use pthreads you may have a look at mutexes). A thread then acquires a lock just before the output loop and frees the lock after.

If there is a computation in the loop, this may lead to performance issues since threads will have to wait for each other. If you just output an array i don't expect any performance disadvantages compared to the sprintf solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.