Question

In C Programming: Thread safety is a concept often used when discussing subroutines or other code...

In C Programming:

Thread safety is a concept often used when discussing subroutines or other code segments that are written in such way that they can be “multiply run” as parts of multiple threads that are being scheduled concurrently or otherwise multiprogrammed. Do you think that the function printf() is thread safe? Do some research and/or run some code experiments and provide a brief, but complete, answer that explains your view on the subject. Be sure to explain why you hold your position on the subject, no matter what that is.

Homework Answers

Answer #1

Do you think that the function prinyf() is thread safe?

if you use printf simultaneously from multiple threads. ... They are both thread-safe to the point that your application won't crash if multiple threads call them on the same file descriptor. However, without some application-level locking, whatever is written could be interleaved.

Example:

char arr[10];

int index=0;

int func(char c)

{

int i=0;

if(index >= sizeof(arr))

{

printf("\n No storage\n");

return -1;

}

This is an example of THREAD SAFE

/* ...

Lock a mutex here

...

*/

arr[index] = c;

index++;

/* ...

unlock the mutex here

...

*/

return index;

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions