Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to suppress bu_log from multiple threads simultaneously #100

Open
kennethsdavis opened this issue Oct 25, 2023 · 5 comments
Open

Comments

@kennethsdavis
Copy link
Contributor

kennethsdavis commented Oct 25, 2023

I am using the BRL-CAD API in a multi-threaded application and would like to suppress all log messages printed to the screen by bu_log. Calling bu_log_hook_delete_all and bu_log_add_hook with an empty function is a near solution. However, if one thread is inside the call to log_call_hooks when a second thread calls bu_log, then the second thread will have its message printed to the screen.

Providing a function that silences bu_log would solve the problem. Though it might be nice to make bu_hook_call thread-safe.

BRL-CAD 7.32.2

@kennethsdavis
Copy link
Contributor Author

kennethsdavis commented Oct 25, 2023

To reproduce the error on windows and linux, build and run the following code.

#include <algorithm>
#include <thread>
#include <vector>

#include <brlcad.h>

void thread_function()
{
   while(true)
   {
      bu_log("bu_log: thread log message\n");
      std::this_thread::sleep_for(std::chrono::milliseconds(10));
   }
};

int main(int argc, char** argv)
{
   bu_log("bu_log: test log message\n");

   // after this call to bu_log_add_hook no calls to bu_log should result in a
   // message being printed
   bu_log_add_hook([](void*,void*) { return 0; }, NULL);

   std::vector<std::thread> threads;

   // set thread_count to 1 to verify that bu_log doesn't print any messages in
   // a single-threaded context
   const unsigned int thread_count = std::thread::hardware_concurrency();
   std::generate_n(std::back_inserter(threads), thread_count, []() { return std::thread(thread_function); });

   // because the thread_function has an infinite loop the only way to quit is
   // to CTRL-C your way out of the application. all remaining code is
   // effectively useless.
   std::for_each(std::begin(threads), std::end(threads), [](auto& thread) { thread.join(); });

   return EXIT_SUCCESS;
}

@Walker0710
Copy link

hello i'm a beginner, can you please assign me some beginner level issues or any bugs.

@brlcad
Copy link
Member

brlcad commented Dec 4, 2023

@AnkushSingh0710 Since you're totally commenting on this thread seemingly randomly -- you could try to address this issue.

@kennethsdavis Thank you for the report. I'd be very interested to hear more about this multithreaded application of yours. That said, thank you very much for the code snippet that demonstrates this issue and the detailed writeup. I agree that pattern should work the way you're calling without erroneous logging and is quite unexpected. I'm guessing we don't see it in our own applications because logging is not suppressed by default. There certainly isn't a unit test for it (as yet).

@kennethsdavis
Copy link
Contributor Author

I’m working on an impact simulation. We’re using the brlcad API to fire thousands of rays through models to characterize the material overlap between them. All this can happen from multiple perspectives at once. It’s been an adventure getting this to work.

@kennethsdavis
Copy link
Contributor Author

@brlcad How do I get this issue assigned to me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants