-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread.cpp
95 lines (72 loc) · 2.25 KB
/
Thread.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/***************************************************************************
The Base Framework (Test Suite)
A framework for developing platform independent applications
See COPYRIGHT.txt for details.
This framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the licensing terms refer to the file 'LICENSE'.
***************************************************************************/
#include <base/Application.h>
#include <base/string/FormatOutputStream.h>
#include <base/concurrency/Thread.h>
#include <base/concurrency/ExclusiveSynchronize.h>
using namespace com::azure::dev::base;
SpinLock lock;
class MyThread : public Runnable {
private:
char value = 0;
unsigned int count = 0;
public:
MyThread(char _value, unsigned int _count)
: value(_value), count(_count)
{
}
void run()
{
Thread::setThreadName("MyThread");
{
ExclusiveSynchronize<SpinLock> exclusiveSyncrhonize(lock);
fout << "Written by MyThread object" << ENDL;
}
while (count--) {
{
ExclusiveSynchronize<SpinLock> exclusiveSyncrhonize(lock);
fout << value;
}
Thread::yield();
}
}
};
class ThreadApplication : public Application {
private:
static const unsigned int MAJOR_VERSION = 1;
static const unsigned int MINOR_VERSION = 0;
public:
ThreadApplication()
: Application(MESSAGE("Thread"))
{
}
void main()
{
#if (_COM_AZURE_DEV__BASE__OS == _COM_AZURE_DEV__BASE__WASI) || \
(_COM_AZURE_DEV__BASE__ARCH == _COM_AZURE_DEV__BASE__SPARC64) // setup_rt_frame: not implemented
return;
#endif
fout << getFormalName() << " version "
<< MAJOR_VERSION << '.' << MINOR_VERSION << EOL
<< "The Base Framework (Test Suite)" << EOL
<< ENDL;
MyThread myThreadA('A', 4096);
MyThread myThreadB('B', 4096);
Thread myContextA(&myThreadA);
Thread myContextB(&myThreadB);
fout << "Starting threads..." << ENDL;
myContextA.start();
myContextB.start();
fout << "Waiting for threads to complete..." << ENDL;
myContextA.join();
myContextB.join();
}
};
APPLICATION_STUB(ThreadApplication);