BRL-CAD / brlcad

BRL-CAD is a powerful cross-platform open source combinatorial solid modeling system
https://brlcad.org
Other
686 stars 140 forks source link

unable to suppress bu_log from multiple threads simultaneously #100

Open kennethsdavis opened 10 months ago

kennethsdavis commented 10 months ago

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 commented 10 months ago

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 commented 9 months ago

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

brlcad commented 9 months ago

@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 commented 9 months ago

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 commented 9 months ago

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