ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My program is formed by three threads. What happens if all these threads have write access to a log file? What would be the worst case? I don't care if one thread writes its log before the others but I'm afraid of loosing any log. I didn't want to develop a complicated mechanism just to write logs
My program is formed by three threads. What happens if all these threads have write access to a log file? What would be the worst case? I don't care if one thread writes its log before the others but I'm afraid of loosing any log. I didn't want to develop a complicated mechanism just to write logs
your log file may be wrong. you can use mutexes(pthread_mutex_lock/unlock) or flock.
while lock file exists
wait
end while
touch lock file
write logs
delete lock file
That way you will not lose any log records. However this is really only acceptable if writing to log records is infrequent, otherwise it might lead to one thread bing locked out.
Anything that threads do with respect to any shared object should be synchronized explicitly. The amount of time needed to do that is completely inconsequential (the I/O operation dwarfs it), and for reliability it's essential.
You might consider whether you want multiple threads to be writing to the same file anyway. That's a "big, fat, slow thing" to be synchronizing against and it could easily wipe out whatever advantages having multiple threads may give you!
I have a last question: what about calling a function from multimple threads? This function would be defined in my code and doesn't alter any shared object.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Originally Posted by graemef
You can establish a fairly simple locking system.
Code:
while lock file exists
wait
end while
touch lock file
write logs
delete lock file
That way you will not lose any log records. However this is really only acceptable if writing to log records is infrequent, otherwise it might lead to one thread bing locked out.
There is still a risk of simultaneous writes with this code, as the control can be passed from one thread to the other just before the former was about to create the lock file, resulting in two threads believing they own the log file at the same time.
This risk is higher with multicore/multi CPU O/Ses.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.