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.
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;
}
Get Answers For Free
Most questions answered within 1 hours.