Skip to content

Commit

Permalink
Replaced Boost.Thread synchronization primitives with std equivalents.
Browse files Browse the repository at this point in the history
This replaces boost::thread and most mutexes and condition variables with
std equivalents. It also adds support for std lock types to the strictest_lock
type trait.

This significantly, although not completely, reduces the dependency on
Boost.Thread.

Refs #232.
  • Loading branch information
Lastique committed Aug 16, 2024
1 parent 9a906e2 commit 80d90de
Show file tree
Hide file tree
Showing 55 changed files with 402 additions and 386 deletions.
9 changes: 9 additions & 0 deletions doc/changelog.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@

[section:changelog Changelog]

[heading 2.30, Boost 1.87]

* Replaced __boost_thread__ synchronization primitives with equivalents from the C++ standard library. This may improve multithreaded performance, but also has user-facing consequences:
* __boost_thread__ thread interruption is no longer supported. Boost.Log no longer has special treatment for the `thread_interrupted` exception that is used by Boost.Thread to implement thread interruption. This exception will be handled like any other exception.
In particular, user-specified exception handlers may now be invoked with the `thread_interrupted` pending exception.
* For timed waiting operations, timeouts are now using std::chrono time units. This means that the `ordering_window` named parameter that is supported by the [class_sinks_bounded_ordering_queue] and [class_sinks_unbounded_ordering_queue] now expects an `std::chrono::duration` value instead of `boost::posix_time::time_duration` from __boost_date_time__.
* In case of errors indicated by thread synchronization primitives, `std::system_error` exception is thrown instead of __boost_thread__ exception types.
* Added support for C++ standard library lock types to [class_log_strictest_lock].

[heading 2.29, Boost 1.86]

* Added a workaround for `windres.exe` issue, when it is used in CMake to compile event log resource files on MinGW-w64. ([pull_request 231])
Expand Down
1 change: 0 additions & 1 deletion example/advanced_usage/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ project
<library>/boost/log//boost_log
<library>/boost/date_time//boost_date_time
<library>/boost/filesystem//boost_filesystem
<library>/boost/thread//boost_thread
<threading>multi
;

Expand Down
1 change: 0 additions & 1 deletion example/async_log/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ project
<library>/boost/log//boost_log
<library>/boost/date_time//boost_date_time
<library>/boost/filesystem//boost_filesystem
<library>/boost/thread//boost_thread
<threading>multi
;

Expand Down
24 changes: 11 additions & 13 deletions example/async_log/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
// #define BOOST_LOG_DYN_LINK 1

#include <stdexcept>
#include <thread>
#include <string>
#include <iostream>
#include <fstream>
#include <functional>
#include <boost/core/ref.hpp>
#include <boost/bind/bind.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <boost/compat/latch.hpp>

#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
Expand All @@ -51,13 +48,13 @@ enum
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::logger_mt)

//! This function is executed in multiple threads
void thread_fun(boost::barrier& bar)
void thread_fun(boost::compat::latch& latch)
{
// Wait until all threads are created
bar.wait();
latch.arrive_and_wait();

// Here we go. First, identify the thread.
BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", std::this_thread::get_id());

// Now, do some logging
for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
Expand Down Expand Up @@ -95,7 +92,7 @@ int main(int argc, char* argv[])
expr::format("%1%: [%2%] [%3%] - %4%")
% expr::attr< unsigned int >("RecordID")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::attr< boost::thread::id >("ThreadID")
% expr::attr< std::thread::id >("ThreadID")
% expr::smessage
);

Expand All @@ -107,13 +104,14 @@ int main(int argc, char* argv[])
logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());

// Create logging threads
boost::barrier bar(THREAD_COUNT);
boost::thread_group threads;
boost::compat::latch latch(THREAD_COUNT);
std::thread threads[THREAD_COUNT];
for (unsigned int i = 0; i < THREAD_COUNT; ++i)
threads.create_thread(boost::bind(&thread_fun, boost::ref(bar)));
threads[i] = std::thread([&latch]() { thread_fun(latch); });

// Wait until all action ends
threads.join_all();
for (unsigned int i = 0; i < THREAD_COUNT; ++i)
threads[i].join();

// Flush all buffered records
sink->stop();
Expand Down
1 change: 0 additions & 1 deletion example/basic_usage/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ project
<library>/boost/date_time//boost_date_time
<library>/boost/filesystem//boost_filesystem
<threading>single:<define>BOOST_LOG_NO_THREADS
<threading>multi:<library>/boost/thread//boost_thread
;

exe basic_usage
Expand Down
1 change: 0 additions & 1 deletion example/bounded_async_log/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ project
<library>/boost/log//boost_log
<library>/boost/date_time//boost_date_time
<library>/boost/filesystem//boost_filesystem
<library>/boost/thread//boost_thread
<threading>multi
;

Expand Down
24 changes: 11 additions & 13 deletions example/bounded_async_log/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
// #define BOOST_LOG_DYN_LINK 1

#include <stdexcept>
#include <thread>
#include <string>
#include <iostream>
#include <fstream>
#include <functional>
#include <boost/core/ref.hpp>
#include <boost/bind/bind.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <boost/compat/latch.hpp>

#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
Expand All @@ -51,13 +48,13 @@ enum
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::logger_mt)

//! This function is executed in multiple threads
void thread_fun(boost::barrier& bar)
void thread_fun(boost::compat::latch& latch)
{
// Wait until all threads are created
bar.wait();
latch.arrive_and_wait();

// Here we go. First, identify the thread.
BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", std::this_thread::get_id());

// Now, do some logging
for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
Expand Down Expand Up @@ -97,7 +94,7 @@ int main(int argc, char* argv[])
expr::format("%1%: [%2%] [%3%] - %4%")
% expr::attr< unsigned int >("RecordID")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::attr< boost::thread::id >("ThreadID")
% expr::attr< std::thread::id >("ThreadID")
% expr::smessage
);

Expand All @@ -109,13 +106,14 @@ int main(int argc, char* argv[])
logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());

// Create logging threads
boost::barrier bar(THREAD_COUNT);
boost::thread_group threads;
boost::compat::latch latch(THREAD_COUNT);
std::thread threads[THREAD_COUNT];
for (unsigned int i = 0; i < THREAD_COUNT; ++i)
threads.create_thread(boost::bind(&thread_fun, boost::ref(bar)));
threads[i] = std::thread([&latch]() { thread_fun(latch); });

// Wait until all action ends
threads.join_all();
for (unsigned int i = 0; i < THREAD_COUNT; ++i)
threads[i].join();

// Flush all buffered records
sink->stop();
Expand Down
4 changes: 3 additions & 1 deletion example/doc/extension_app_launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <utility>
#include <stdexcept>
#include <boost/core/ignore_unused.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/phoenix.hpp>
#include <boost/log/trivial.hpp>
Expand Down Expand Up @@ -47,7 +48,8 @@ class app_launcher :
// The function consumes the log records that come from the frontend
void app_launcher::consume(logging::record_view const& rec, string_type const& command_line)
{
std::system(command_line.c_str());
int res = std::system(command_line.c_str());
boost::ignore_unused(res);
}
//]

Expand Down
4 changes: 2 additions & 2 deletions example/doc/extension_record_tagger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* http://www.boost.org/LICENSE_1_0.txt)
*/

#include <mutex>
#include <string>
#include <ostream>
#include <fstream>
Expand All @@ -13,7 +14,6 @@
#include <boost/scope_exit.hpp>
#include <boost/mpl/quote.hpp>
#include <boost/parameter/keyword.hpp>
#include <boost/thread/locks.hpp>
#include <boost/move/utility_core.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
Expand Down Expand Up @@ -65,7 +65,7 @@ class record_tagger_feature :
// The method will require locking, so we have to define locking requirements for it.
// We use the strictest_lock trait in order to choose the most restricting lock type.
typedef typename logging::strictest_lock<
boost::lock_guard< threading_model >,
std::lock_guard< threading_model >,
typename BaseT::open_record_lock,
typename BaseT::add_attribute_lock,
typename BaseT::remove_attribute_lock
Expand Down
4 changes: 2 additions & 2 deletions example/doc/sinks_async_ordering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* http://www.boost.org/LICENSE_1_0.txt)
*/

#include <chrono>
#include <string>
#include <fstream>
#include <iostream>
#include <functional>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/async_frontend.hpp>
Expand Down Expand Up @@ -62,7 +62,7 @@ boost::shared_ptr< sink_t > init_logging()
backend, /*< pointer to the pre-initialized backend >*/
keywords::order = logging::make_attr_ordering< unsigned int >( /*< log record ordering predicate >*/
"LineID", std::less< unsigned int >()),
keywords::ordering_window = boost::posix_time::seconds(1) /*< latency of log record processing >*/
keywords::ordering_window = std::chrono::seconds(1) /*< latency of log record processing >*/
));
core->add_sink(sink);

Expand Down
4 changes: 4 additions & 0 deletions example/doc/util_dynamic_type_disp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstddef>
#include <string>
#include <iostream>
#include <boost/core/ignore_unused.hpp>
#include <boost/log/utility/type_dispatch/dynamic_type_dispatcher.hpp>

namespace logging = boost::log;
Expand Down Expand Up @@ -85,13 +86,16 @@ int main(int, char*[])
// These two attributes are supported by the dispatcher
bool res = print(my_value< std::string >("Hello world!"));
assert(res);
boost::ignore_unused(res);

res = print(my_value< double >(1.2));
assert(res);
boost::ignore_unused(res);

// This one is not
res = print(my_value< float >(-4.3f));
assert(!res);
boost::ignore_unused(res);

return 0;
}
4 changes: 4 additions & 0 deletions example/doc/util_static_type_disp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstddef>
#include <string>
#include <iostream>
#include <boost/core/ignore_unused.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/log/utility/type_dispatch/static_type_dispatcher.hpp>

Expand Down Expand Up @@ -82,13 +83,16 @@ int main(int, char*[])
// These two attributes are supported by the dispatcher
bool res = print(my_value< std::string >("Hello world!"));
assert(res);
boost::ignore_unused(res);

res = print(my_value< double >(1.2));
assert(res);
boost::ignore_unused(res);

// This one is not
res = print(my_value< float >(-4.3f));
assert(!res);
boost::ignore_unused(res);

return 0;
}
1 change: 0 additions & 1 deletion example/event_log/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ project
<library>/boost/log//boost_log
<library>/boost/date_time//boost_date_time
<library>/boost/filesystem//boost_filesystem
<library>/boost/thread//boost_thread
<threading>multi
;

Expand Down
1 change: 0 additions & 1 deletion example/keywords/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ project
<library>/boost/date_time//boost_date_time
<library>/boost/filesystem//boost_filesystem
<threading>single:<define>BOOST_LOG_NO_THREADS
<threading>multi:<library>/boost/thread//boost_thread
;

exe keywords
Expand Down
1 change: 0 additions & 1 deletion example/multiple_files/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ project
<library>/boost/log//boost_log
<library>/boost/date_time//boost_date_time
<library>/boost/filesystem//boost_filesystem
<library>/boost/thread//boost_thread
<threading>multi
;

Expand Down
13 changes: 7 additions & 6 deletions example/multiple_files/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
// #define BOOST_LOG_DYN_LINK 1

#include <stdexcept>
#include <thread>
#include <string>
#include <iostream>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <boost/log/common.hpp>
Expand Down Expand Up @@ -53,7 +53,7 @@ BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
// This function is executed in a separate thread
void thread_foo()
{
BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", std::this_thread::get_id());
for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
{
BOOST_LOG(my_logger::get()) << "Log record " << i;
Expand All @@ -70,7 +70,7 @@ int main(int argc, char* argv[])

// Set up how the file names will be generated
sink->locked_backend()->set_file_name_composer(sinks::file::as_file_name_composer(
expr::stream << "logs/" << expr::attr< boost::thread::id >("ThreadID") << ".log"));
expr::stream << "logs/" << expr::attr< std::thread::id >("ThreadID") << ".log"));

// Set the log record formatter
sink->set_formatter
Expand All @@ -89,11 +89,12 @@ int main(int argc, char* argv[])
logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());

// Create threads and make some logs
boost::thread_group threads;
std::thread threads[THREAD_COUNT];
for (unsigned int i = 0; i < THREAD_COUNT; ++i)
threads.create_thread(&thread_foo);
threads[i] = std::thread(&thread_foo);

threads.join_all();
for (unsigned int i = 0; i < THREAD_COUNT; ++i)
threads[i].join();

return 0;
}
Expand Down
1 change: 0 additions & 1 deletion example/multiple_threads/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ project
<library>/boost/log//boost_log
<library>/boost/date_time//boost_date_time
<library>/boost/filesystem//boost_filesystem
<library>/boost/thread//boost_thread
<threading>multi
;

Expand Down
Loading

0 comments on commit 80d90de

Please sign in to comment.