回答編集履歴

1

抜粋コードの追加

2017/06/29 04:24

投稿

tamy
tamy

スコア442

test CHANGED
@@ -1,3 +1,67 @@
1
1
  `CThreadManagerTest::Run`の中で,`m_bAlive = true`とする必要があるのではないでしょうか.
2
2
 
3
3
  それをしないと,遅延評価では`m_bAlive`がずっと`false`のままなので,`CThreadManagerTest::Interrupt`内の条件分岐で弾かれる気がします.
4
+
5
+
6
+
7
+ 以下,該当部分だけ抜粋させていただきます.
8
+
9
+
10
+
11
+ ```c++
12
+
13
+ // コンストラクタ1. 関数を引き受けて起動. bool bDelay が true なら遅延起動.
14
+
15
+ CThreadManagerTest(boost::function<void (void)> func, int threadID = 1, bool bDelay = false)
16
+
17
+ : m_func(func), m_threadID(threadID), m_bDelay(bDelay)
18
+
19
+ {
20
+
21
+ if( !m_bDelay ){
22
+
23
+ this->m_th = boost::thread( func );
24
+
25
+ this->m_bAlive = true;
26
+
27
+ } else {
28
+
29
+ this->m_bAlive = false; // ここで false になる
30
+
31
+ }
32
+
33
+ }
34
+
35
+
36
+
37
+ // Runメンバ関数. 遅延起動時 効果あり.
38
+
39
+ void Run(void)
40
+
41
+ {
42
+
43
+ if( this->m_bDelay && !this->m_func.empty() ){
44
+
45
+ this->m_th = boost::thread( m_func );
46
+
47
+ // ここで m_bAlive = true にしないといけない
48
+
49
+ }
50
+
51
+ }
52
+
53
+
54
+
55
+ // Interruptメンバ関数. 別スレッドに"終了"を指令. 別スレッドは InterruptPoint非メンバ関数でチェックする.
56
+
57
+ void Interrupt(void)
58
+
59
+ {
60
+
61
+ // Run 内で m_bAlive を true にしていないので,m_th.interrupt() は実行されない
62
+
63
+ if( this->m_bAlive ) m_th.interrupt();
64
+
65
+ }
66
+
67
+ ```