Experimenting with lamda and threads.
David Blume

David Blume commited on 2016-01-10 18:20:39
Showing 5 changed files, with 57 additions and 4 deletions.

... ...
@@ -1,5 +1,5 @@
1 1
 CXX=g++
2
-CPPFLAGS=-c -Wall -std=c++11 -I../include
2
+CPPFLAGS=-c -Wall -std=c++14 -I../include
3 3
 LDFLAGS=
4 4
 OBJDIR=obj
5 5
 LIBDIR=../lib
... ...
@@ -1,10 +1,11 @@
1 1
 CXX=g++
2
-CPPFLAGS=-c -Wall -std=c++11 -I../include
2
+CPPFLAGS=-c -Wall -std=c++14 -I../include
3 3
 LIBDIR=../lib
4
-LDFLAGS=-L $(LIBDIR) -lmy_lib
4
+LDFLAGS=-pthread -L $(LIBDIR) -lmy_lib
5 5
 OBJDIR=obj
6 6
 
7
-SOURCES=main.cpp main_helper.cpp ui/widget.cpp scoped_set_adder.cpp
7
+SOURCES=main.cpp main_helper.cpp ui/widget.cpp \
8
+	scoped_set_adder.cpp thread_with_lambda.cpp
8 9
 # //OBJECTS=$(OBJDIR)$(SOURCES:.cpp=.o)
9 10
 OBJECTS=$(foreach bname, $(basename $(SOURCES)), $(OBJDIR)/$(bname).o)
10 11
 EXECUTABLE=main
... ...
@@ -4,10 +4,12 @@
4 4
 #include <iostream>
5 5
 #include <algorithm>
6 6
 #include <memory>
7
+#include <complex>
7 8
 #include "main_helper.hpp"
8 9
 #include "scoped_set_adder.hpp"
9 10
 #include "ui/widget.hpp"
10 11
 #include "../include/my_lib.hpp"
12
+#include "thread_with_lambda.hpp"
11 13
  
12 14
 using namespace std;
13 15
  
... ...
@@ -50,10 +52,27 @@ int remove_duplicates(int a[], int n)
50 52
     return last + 1;
51 53
 }
52 54
 
55
+void test_cpp14() {
56
+    // Store a generalized lambda, that squares a number, in a variable
57
+    auto func = [](auto input) { return input * input; };
58
+
59
+    // square of an int
60
+    std::cout << func(10) << std::endl;
61
+
62
+    // square of a double
63
+    std::cout << func(2.345) << std::endl;
64
+
65
+    // square of a complex number
66
+    std::cout << func(std::complex<double>(3, -2)) << std::endl;
67
+}
68
+ 
53 69
 int main() {
54 70
     length_of_array = 6;
55 71
     int a[6] = { 10, 20, 20, 30, 40, 40 };
56 72
     int item_to_remove = 20;
73
+
74
+    // thread_with_lambda();
75
+
57 76
     cout << "Removing " << item_to_remove << endl;
58 77
     length_of_array = remove_element(a, length_of_array, item_to_remove);
59 78
     cout << "a became " << a << endl;
... ...
@@ -68,9 +87,11 @@ int main() {
68 87
 	cout << "sizeof(int) is " << sizeof(int) << "." << endl;
69 88
 	my_lib_fn();
70 89
 	my_lib_fn(5);
90
+    test_cpp14();
71 91
 
72 92
 	{
73 93
 		std::unique_ptr<int> up( new int() );
94
+		scoped_set_adder set_adder("testing unique_ptr");
74 95
 		if (up) {
75 96
 		    cout << "unique_ptr passes if." << endl;
76 97
 		} else {
... ...
@@ -0,0 +1,25 @@
1
+#include <thread>
2
+#include <mutex>
3
+#include <iostream>
4
+#include "thread_with_lambda.hpp"
5
+
6
+
7
+namespace {
8
+
9
+std::mutex my_mutex;
10
+std::thread worker;
11
+
12
+}
13
+
14
+void thread_with_lambda()
15
+{
16
+    uint32_t ticks = 10;
17
+    std::lock_guard<std::mutex> lock(my_mutex);
18
+    if (worker.joinable()) worker.join();
19
+
20
+    worker = std::thread([=]{
21
+        for(uint32_t tick = 0; tick <= ticks; tick++) {
22
+            std::cout << "count " << tick << std::endl;
23
+        }
24
+    });
25
+}
... ...
@@ -0,0 +1,6 @@
1
+#ifndef thread_with_lambda_hpp_
2
+#define thread_with_lambda_hpp_
3
+
4
+void thread_with_lambda();
5
+
6
+#endif
0 7