"Boost.Thread enables the use of multiple threads of execution with
shared data in portable C++ code. It provides classes and functions for
managing the threads themselves, along with others for synchronizing
data between the threads or providing separate copies of data specific
to individual threads."
Briefly
In concurrent computing, it can perform multiple tasks
simultaneously. Boost.Thread is a famous library in the Boost C++
libraries that provides a platform-independent interface to threading
and synchronization facilities. It provides a wide range of facilities
for managing threads.
In this post, I would try passing through the modeling a bank account
class that supports simultaneously deposits and withdrawals from
multiple locations. Also, setup libboost_thread.so.1.80.0 environment in
a Docker container to build boost concurrent execution of a
component.
This is an example of moduling a bank account class that supports
simultaneously deposits and withdrawals from multiple locations object
implementation.
void bankAgent() { for (int i = 10; i > 0; i --) { JoesAccount.Deposit(500); } }
void Joe() { for (int i = 10; i > 0; --i) { // error: void value not ignored as it ought to be // int mypocket = JoesAccount.Withdraw(100);
// The correct way JoesAccount.Withdraw(100); int mypocket = JoesAccount.GetBalance(); std::cout << mypocket << std::endl; } }
int main() { boost::thread thread1(bankAgent); // start concurrent execution of bankAgent boost::thread thread2(Joe); // start concurrent execution of Joe thread1.join(); thread2.join(); return 0; }
To build the boost thread example using CMake, we can run the
following command.
1 2
$ cmake -B build $ cmake --build build
View the Stack for Every
Threads
By viewing the stack for every threads, it shows how the balance in
bank account changes from time to time.
Here we set the breakpints where bankAgent deposits and where Joe
withdraws from bank account.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
root@0518fede6045:/mnt# gdb build/boost_threads/BankAccount GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1 Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://aus01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fgnu.org%2Flicenses%2Fgpl.html&data=05%7C01%7C%7C05bc8a5436354e8ec9a508db0d8577e3%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638118639120585581%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=w1lRuyMBTUmuRsKkUaKnlOw2TeVmIAraujdPQmCsuzA%3D&reserved=0> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. ...
For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from build/boost_threads/BankAccount...(no debugging symbols found)...done. (gdb) b BankAccount::Deposit Breakpoint 1 at 0x41249e (gdb) b BankAccount::Withdraw Breakpoint 2 at 0x4124dc ...
Here we can type thread apply all backtrace full, which display the
backtrace for all the threads; this is helpful for observing a core dump
of a multi-threaded program.
(gdb) r Starting program: /mnt/build/boost_threads/BankAccount warning: Error disabling address space randomization: Operation not permitted [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". [New Thread 0x7f5140c35700 (LWP 18)] [New Thread 0x7f5140434700 (LWP 19)] [Switching to Thread 0x7f5140c35700 (LWP 18)]
Thread 2 "BankAccount" hit Breakpoint 1, 0x000000000041249e in BankAccount::Deposit(int) () (gdb) n Single stepping until exit from function _ZN11BankAccount7DepositEi, which has no line number information. [Switching to Thread 0x7f5140434700 (LWP 19)]
Thread 3 "BankAccount" hit Breakpoint 2, 0x00000000004124dc in BankAccount::Withdraw(int) () (gdb) Single stepping until exit from function _ZN11BankAccount8WithdrawEi, which has no line number information. [Switching to Thread 0x7f5140c35700 (LWP 18)]
Thread 2 "BankAccount" hit Breakpoint 1, 0x000000000041249e in BankAccount::Deposit(int) () (gdb) Single stepping until exit from function _ZN11BankAccount7DepositEi, which has no line number information. 400 [Switching to Thread 0x7f5140434700 (LWP 19)]
Thread 3 "BankAccount" hit Breakpoint 2, 0x00000000004124dc in BankAccount::Withdraw(int) () (gdb) thread apply all bt full
Thread 3 (Thread 0x7f5140434700 (LWP 19)): #0 0x00000000004124dc in BankAccount::Withdraw(int) () No symbol table info available. #1 0x000000000040f807 in Joe() () No symbol table info available. #2 0x0000000000415301 in boost::detail::thread_data<void (*)()>::run() () No symbol table info available. #3 0x00007f5141ac7075 in thread_proxy () from /usr/local/lib/libboost_thread.so.1.80.0 No symbol table info available. #4 0x00007f51418a86ba in start_thread (arg=0x7f5140434700) at pthread_create.c:333 __res = <optimized out> pd = 0x7f5140434700 now = <optimized out> unwind_buf = {cancel_jmp_buf = {{jmp_buf = {139986947229440, 6099298229468553489, 0, 140737064342143, 139986947230144, 0, -6194824523399536367, -6194828308583128815}, mask_was_saved = 0}}, priv = {pad = {0x0, 0x0, 0x0, 0x0}, data = {prev = 0x0, cleanup = 0x0, canceltype = 0}}} not_first_call = <optimized out> pagesize_m1 = <optimized out> sp = <optimized out> freesize = <optimized out> __PRETTY_FUNCTION__ = "start_thread" #5 0x00007f514104651d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109 No locals.
Thread 2 (Thread 0x7f5140c35700 (LWP 18)): #0 0x00000000004124a2 in BankAccount::Deposit(int) () No symbol table info available. #1 0x000000000040f7da in bankAgent() () No symbol table info available. #2 0x0000000000415301 in boost::detail::thread_data<void (*)()>::run() () No symbol table info available. #3 0x00007f5141ac7075 in thread_proxy () from /usr/local/lib/libboost_thread.so.1.80.0 No symbol table info available. #4 0x00007f51418a86ba in start_thread (arg=0x7f5140c35700) at pthread_create.c:333 __res = <optimized out> pd = 0x7f5140c35700 now = <optimized out> unwind_buf = {cancel_jmp_buf = {{jmp_buf = {139986955622144, 6099298229468553489, 0, 140737064342143, 139986955622848, 0, -6194825626669260527, -6194828308583128815}, mask_was_saved = 0}}, priv = {pad = {0x0, 0x0, 0x0, 0x0}, data = {prev = 0x0, cleanup = 0x0, canceltype = 0}}} not_first_call = <optimized out> pagesize_m1 = <optimized out> sp = <optimized out> freesize = <optimized out> __PRETTY_FUNCTION__ = "start_thread" #5 0x00007f514104651d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109 No locals.
Thread 1 (Thread 0x7f5141ef2740 (LWP 14)): #0 pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185 No locals. #1 0x00007f5141accd4b in boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) () from /usr/local/lib/libboost_thread.so.1.80.0 No symbol table info available. #2 0x00007f5141ac71c4 in boost::thread::join_noexcept() () from /usr/local/lib/libboost_thread.so.1.80.0 No symbol table info available. #3 0x0000000000412220 in boost::thread::join() () ---Type <return> to continue, or q <return> to quit--- No symbol table info available. #4 0x000000000040f87f in main () No symbol table info available. ...
So from running the code, this stack shows that the bankAgent and Joe
are executed concurrently.
... Thread 2 "BankAccount" hit Breakpoint 1, 0x000000000041249e in BankAccount::Deposit(int) () (gdb) Single stepping until exit from function _ZN11BankAccount7DepositEi, which has no line number information. 3200 [Switching to Thread 0x7f5140434700 (LWP 19)]
Thread 3 "BankAccount" hit Breakpoint 2, 0x00000000004124dc in BankAccount::Withdraw(int) () (gdb) Single stepping until exit from function _ZN11BankAccount8WithdrawEi, which has no line number information. [Switching to Thread 0x7f5140c35700 (LWP 18)]
Thread 2 "BankAccount" hit Breakpoint 1, 0x000000000041249e in BankAccount::Deposit(int) () (gdb) Single stepping until exit from function _ZN11BankAccount7DepositEi, which has no line number information. 3600 [Switching to Thread 0x7f5140434700 (LWP 19)]
Thread 3 "BankAccount" hit Breakpoint 2, 0x00000000004124dc in BankAccount::Withdraw(int) () (gdb) Single stepping until exit from function _ZN11BankAccount8WithdrawEi, which has no line number information. [Thread 0x7f5140c35700 (LWP 18) exited] 0x000000000040f807 in Joe() () (gdb) Single stepping until exit from function _Z3Joev, which has no line number information. 4000 0x0000000000415301 in boost::detail::thread_data<void (*)()>::run() () (gdb) ...
Due to the most time on my job, we need to type many git command
lines. So that this article is used to sort several git commands
frequently used for easily searching.
"Most players gain pleasure from feeling accepted or belonging to the
group. The good player however, gains pleasure from his ability to cope
with the realities of the game"
In our daily life, most of us usually use Google Map or Apple Maps to help us to
navigate to a place. We can see traffic info, public transit and also
choose our mode of transportation.
"After going on your life-changing journey, you now realize that you
don’t want what you thought you wanted. What you really wanted was
inside you all along."
The HTML canvas can be used for sketching/drawing by mouse. The
canvas API provides a means for drawing graphics via JavaScript and the
HTML element. It largely focuses on 2D graphics. In this post, I would
like to describe how to quickly go about implementing this.
The HTML canvas can be used for sketching/drawing either by mouse or by touch. In the previous post, I quickly discuss how to use Canvas API to implement a canvas with mouse sketching. In this post, I would like to describe how go about implementing for sketching/drawing by mouse and touch.
Either computer data storage or telecommunication, regardless of the
data storages and transmission, is non-zero probabilities that the data
could be changed while it's being stored or transmitted. There is always
a code-word with block length without free bit-errors. That means the
data probably could be changed while it is being processed or
transmitted. If the machine can't locate the position of the error and
correct it, the information might be lost forever.
"Graph theory is the study of graphs, which are mathematical
structures used to model pairwise relations between objects. A graph in
this context is made up of vertices (also called nodes or points) which
are connected by edges (also called links or lines). Graphs are one of
the principal objects of study in discrete mathematics." ... from Wiki
page.