Subj : Re: How to deal with errno in a multipthread program To : comp.programming.threads From : Eric Sosman Date : Fri Apr 15 2005 02:40 pm icoming wrote: > So I even do not need to save errno in a local variable? (Context: Is `int e = errno; f(); errno = e;' required in threaded programs?) You need to save errno if you want to preserve its value while doing something that might change it. If you don't need to preserve the value, you don't need to save it. FILE *old, *new; old = fopen("old.txt", "r"); if (old == NULL) return FAILURE; new = fopen("new.txt", "w"); if (new == NULL) { fclose (old); return FAILURE; } If the FAILURE return is all you care about, this is good enough. If you want to return FAILURE *and* let the caller inspect errno to get more information about the reason for the failure, this may not work because fclose(old) may change errno and thus obliterate any information from the failed fopen(). If you want to preserve that information, write if (new == NULL) { int e = errno; fclose (old); errno = e; return FAILURE; } The decision to preserve errno or not has nothing to do with whether the program is single- or multi-threaded; it's a matter of the "contract" between the function and its callers. -- Eric.Sosman@sun.com .