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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/testapp.h>
using namespace vespalib;
class Test : public TestApp
{
public:
~Test();
int Main() override;
};
Test::~Test()
{
}
void * hammer(void * arg)
{
usleep(4000000);
long seconds = * static_cast<const long *>(arg);
long stopTime(time(NULL) + seconds);
pthread_t id = pthread_self();
while (time(NULL) < stopTime) {
std::vector<pthread_t *> allocations;
for (size_t i(0); i < 2000; i++) {
pthread_t *t = new pthread_t[20];
allocations.push_back(t);
for (size_t j(0); j < 20; j++) {
t[j] = id;
}
}
for (size_t i(0); i < allocations.size(); i++) {
for (size_t j(0); j < 20; j++) {
assert(allocations[i][j] == id);
}
delete [] allocations[i];
}
}
return arg;
}
int Test::Main()
{
TEST_INIT("racemanythreads_test");
size_t threadCount(1024);
long seconds(10);
if (_argc >= 2) {
threadCount = strtoul(_argv[1], NULL, 0);
if (_argc >= 3) {
seconds = strtoul(_argv[2], NULL, 0);
}
}
pthread_attr_t attr;
EXPECT_EQUAL(pthread_attr_init(&attr), 0);
EXPECT_EQUAL(pthread_attr_setstacksize(&attr, 64*1024), 0);
std::vector<pthread_t> threads(threadCount);
for (size_t i(0); i < threadCount; i++) {
EXPECT_EQUAL( pthread_create(&threads[i], &attr, hammer, &seconds), 0);
}
for (size_t i(0); i < threadCount; i++) {
void *retval;
EXPECT_EQUAL(pthread_join(threads[i], &retval), 0);
}
TEST_DONE();
}
TEST_APPHOOK(Test);
|