-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy paththread_ex0.cpp
53 lines (41 loc) · 964 Bytes
/
thread_ex0.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <thread>
using std::cout;
using std::thread;
class A {
public:
A(int v) : val(v) {};
explicit A() : val(10) {};
~A() { cout << "Destroying A Object...\n"; }
void func() {
cout << "func function called ...\n";
}
thread func_thread() {
cout << "\n\nCalling func from a thread ...\n";
return thread( [this] { this->func(); } );
}
private:
int val;
};
void test(std::string msg) {
cout << "test called ... " << msg << std::endl;
}
thread test_threaded() {
cout << "Creating the thread: " << std::endl;
thread t1(test, " OOK !!!");
return t1;
}
int main()
{
/* Scenario I :: OK
A *a_ptr = new A();
cout << "Creating a thread to call func member function ...\n";
thread th( &A::func, a_ptr );
th.join();
delete a_ptr;
*/
/* Scenario II: OK */
thread my_thread = test_threaded();
my_thread.join();
return 0;
}