-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
RefCount_unittest.cc
55 lines (45 loc) · 1.06 KB
/
RefCount_unittest.cc
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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2014 Thoralf Klein
*/
#include <shogun/lib/RefCount.h>
#include <pthread.h>
#include <gtest/gtest.h>
using namespace shogun;
void * stress_test_helper(void * args)
{
RefCount * rc = (RefCount *) args;
for (index_t i = 0; i < 10; i++)
{
rc->ref();
rc->ref();
rc->unref();
rc->unref();
}
pthread_exit(0);
}
TEST(RefCount, stress_test)
{
RefCount * rc = new RefCount(0);
EXPECT_EQ(rc->ref_count(), 0);
rc->ref();
EXPECT_EQ(rc->ref_count(), 1);
pthread_t * threads = new pthread_t[5];
for (index_t i = 0; i < 5; i++)
{
pthread_create(&threads[i], NULL, stress_test_helper, static_cast<void *>(rc));
}
for (index_t i = 0; i < 5; i++)
{
pthread_join(threads[i], NULL);
}
EXPECT_EQ(rc->ref_count(), 1);
rc->unref();
EXPECT_EQ(rc->ref_count(), 0);
delete [] threads;
delete rc;
}