-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of all src and the README.md file.
- Loading branch information
Jonas Tepe
committed
May 16, 2016
1 parent
01fe1a8
commit a319bca
Showing
128 changed files
with
5,728 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Boost Software License - Version 1.0 - August 17th, 2003 | ||
|
||
Permission is hereby granted, free of charge, to any person or organization | ||
obtaining a copy of the software and accompanying documentation covered by | ||
this license (the "Software") to use, reproduce, display, distribute, | ||
execute, and transmit the Software, and to prepare derivative works of the | ||
Software, and to permit third-parties to whom the Software is furnished to | ||
do so, all subject to the following: | ||
|
||
The copyright notices in the Software and this entire statement, including | ||
the above license grant, this restriction and the following disclaimer, | ||
must be included in all copies of the Software, in whole or in part, and | ||
all derivative works of the Software, unless such copies or derivative | ||
works are solely in the form of machine-executable object code generated by | ||
a source language processor. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | ||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | ||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# concurrencyinaction | ||
Concurrency in Action source code examples | ||
# Concurrency in Action source code | ||
This repo just contains the source code of the book [Concurrency in Action](https://www.manning.com/books/c-plus-plus-concurrency-in-action) to easily browse the source. Nothing more, nothing less. Cheers 🍷. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <iostream> | ||
#include <thread> | ||
|
||
void hello() | ||
{ | ||
std::cout<<"Hello Concurrent World\n"; | ||
} | ||
|
||
int main() | ||
{ | ||
std::thread t(hello); | ||
t.join(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
void test_concurrent_push_and_pop_on_empty_queue() | ||
{ | ||
threadsafe_queue<int> q; | ||
|
||
std::promise<void> go,push_ready,pop_ready; | ||
std::shared_future<void> ready(go.get_future()); | ||
|
||
std::future<void> push_done; | ||
std::future<int> pop_done; | ||
|
||
try | ||
{ | ||
push_done=std::async(std::launch::async, | ||
[&q,ready,&push_ready]() | ||
{ | ||
push_ready.set_value(); | ||
ready.wait(); | ||
q.push(42); | ||
} | ||
); | ||
pop_done=std::async(std::launch::async, | ||
[&q,ready,&pop_ready]() | ||
{ | ||
pop_ready.set_value(); | ||
ready.wait(); | ||
return q.pop(); | ||
} | ||
); | ||
push_ready.get_future().wait(); | ||
pop_ready.get_future().wait(); | ||
go.set_value(); | ||
|
||
push_done.get(); | ||
assert(pop_done.get()==42); | ||
assert(q.empty()); | ||
} | ||
catch(...) | ||
{ | ||
go.set_value(); | ||
throw; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <thread> | ||
|
||
void do_something(int& i) | ||
{ | ||
++i; | ||
} | ||
|
||
struct func | ||
{ | ||
int& i; | ||
|
||
func(int& i_):i(i_){} | ||
|
||
void operator()() | ||
{ | ||
for(unsigned j=0;j<1000000;++j) | ||
{ | ||
do_something(i); | ||
} | ||
} | ||
}; | ||
|
||
|
||
void oops() | ||
{ | ||
int some_local_state=0; | ||
func my_func(some_local_state); | ||
std::thread my_thread(my_func); | ||
my_thread.detach(); | ||
} | ||
|
||
int main() | ||
{ | ||
oops(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <thread> | ||
|
||
void do_something(int& i) | ||
{ | ||
++i; | ||
} | ||
|
||
struct func | ||
{ | ||
int& i; | ||
|
||
func(int& i_):i(i_){} | ||
|
||
void operator()() | ||
{ | ||
for(unsigned j=0;j<1000000;++j) | ||
{ | ||
do_something(i); | ||
} | ||
} | ||
}; | ||
|
||
void do_something_in_current_thread() | ||
{} | ||
|
||
void f() | ||
{ | ||
int some_local_state=0; | ||
func my_func(some_local_state); | ||
std::thread t(my_func); | ||
try | ||
{ | ||
do_something_in_current_thread(); | ||
} | ||
catch(...) | ||
{ | ||
t.join(); | ||
throw; | ||
} | ||
t.join(); | ||
} | ||
|
||
int main() | ||
{ | ||
f(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <thread> | ||
|
||
class thread_guard | ||
{ | ||
std::thread& t; | ||
public: | ||
explicit thread_guard(std::thread& t_): | ||
t(t_) | ||
{} | ||
~thread_guard() | ||
{ | ||
if(t.joinable()) | ||
{ | ||
t.join(); | ||
} | ||
} | ||
thread_guard(thread_guard const&)=delete; | ||
thread_guard& operator=(thread_guard const&)=delete; | ||
}; | ||
|
||
void do_something(int& i) | ||
{ | ||
++i; | ||
} | ||
|
||
struct func | ||
{ | ||
int& i; | ||
|
||
func(int& i_):i(i_){} | ||
|
||
void operator()() | ||
{ | ||
for(unsigned j=0;j<1000000;++j) | ||
{ | ||
do_something(i); | ||
} | ||
} | ||
}; | ||
|
||
void do_something_in_current_thread() | ||
{} | ||
|
||
|
||
void f() | ||
{ | ||
int some_local_state; | ||
func my_func(some_local_state); | ||
std::thread t(my_func); | ||
thread_guard g(t); | ||
|
||
do_something_in_current_thread(); | ||
} | ||
|
||
int main() | ||
{ | ||
f(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <thread> | ||
#include <string> | ||
|
||
void open_document_and_display_gui(std::string const& filename) | ||
{} | ||
|
||
bool done_editing() | ||
{ | ||
return true; | ||
} | ||
|
||
enum command_type{ | ||
open_new_document | ||
}; | ||
|
||
|
||
struct user_command | ||
{ | ||
command_type type; | ||
|
||
user_command(): | ||
type(open_new_document) | ||
{} | ||
}; | ||
|
||
user_command get_user_input() | ||
{ | ||
return user_command(); | ||
} | ||
|
||
std::string get_filename_from_user() | ||
{ | ||
return "foo.doc"; | ||
} | ||
|
||
void process_user_input(user_command const& cmd) | ||
{} | ||
|
||
void edit_document(std::string const& filename) | ||
{ | ||
open_document_and_display_gui(filename); | ||
while(!done_editing()) | ||
{ | ||
user_command cmd=get_user_input(); | ||
if(cmd.type==open_new_document) | ||
{ | ||
std::string const new_name=get_filename_from_user(); | ||
std::thread t(edit_document,new_name); | ||
t.detach(); | ||
} | ||
else | ||
{ | ||
process_user_input(cmd); | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
edit_document("bar.doc"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <thread> | ||
|
||
void some_function() | ||
{} | ||
|
||
void some_other_function(int) | ||
{} | ||
|
||
std::thread f() | ||
{ | ||
void some_function(); | ||
return std::thread(some_function); | ||
} | ||
std::thread g() | ||
{ | ||
void some_other_function(int); | ||
std::thread t(some_other_function,42); | ||
return t; | ||
} | ||
|
||
int main() | ||
{ | ||
std::thread t1=f(); | ||
t1.join(); | ||
std::thread t2=g(); | ||
t2.join(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <thread> | ||
#include <utility> | ||
|
||
class scoped_thread | ||
{ | ||
std::thread t; | ||
public: | ||
explicit scoped_thread(std::thread t_): | ||
t(std::move(t_)) | ||
{ | ||
if(!t.joinable()) | ||
throw std::logic_error("No thread"); | ||
} | ||
~scoped_thread() | ||
{ | ||
t.join(); | ||
} | ||
scoped_thread(scoped_thread const&)=delete; | ||
scoped_thread& operator=(scoped_thread const&)=delete; | ||
}; | ||
|
||
void do_something(int& i) | ||
{ | ||
++i; | ||
} | ||
|
||
struct func | ||
{ | ||
int& i; | ||
|
||
func(int& i_):i(i_){} | ||
|
||
void operator()() | ||
{ | ||
for(unsigned j=0;j<1000000;++j) | ||
{ | ||
do_something(i); | ||
} | ||
} | ||
}; | ||
|
||
void do_something_in_current_thread() | ||
{} | ||
|
||
void f() | ||
{ | ||
int some_local_state; | ||
scoped_thread t(std::thread(func(some_local_state))); | ||
|
||
do_something_in_current_thread(); | ||
} | ||
|
||
int main() | ||
{ | ||
f(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <vector> | ||
#include <thread> | ||
#include <algorithm> | ||
#include <functional> | ||
|
||
void do_work(unsigned id) | ||
{} | ||
|
||
void f() | ||
{ | ||
std::vector<std::thread> threads; | ||
for(unsigned i=0;i<20;++i) | ||
{ | ||
threads.push_back(std::thread(do_work,i)); | ||
} | ||
std::for_each(threads.begin(),threads.end(), | ||
std::mem_fn(&std::thread::join)); | ||
} | ||
|
||
int main() | ||
{ | ||
f(); | ||
} |
Oops, something went wrong.