Salut,
Je suis entrain de travailler sur un design pour permettre d'arrêter proprement l'execution de traitement (fonctions de calculs),
suite au declenchement d'une alerte dans un ou plusieurs threads quelquonque.
La seule solution que je vois c'est de checker un booléen partout ou je pourrai dans l'execution des fonctions ( technique utilisé aussi pour les annulations )
quelqu'un a t-il déja employé d'autres techniques plus propres ? ou une version améliorée ?
J'ai mis le code que je viens d'écrire pour illustrer mon propos...
j'utilise la bibliothèque boost pour synchronisé les threads.
ps :
les threads d'alertes invoqueront la méthode cManager::Alert_all() pour arrêter tout...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 class cFunction; class cManager { public : void Alert_All(); protected : void AddFunction(boost::shared_ptr<cFunction>); void RemoveFunction(boost::shared_ptr<cFunction>); private: std::vector<boost::shared_ptr<cFunction> > p_Running; }; void cManager::Alert_All() { for (size_t i = 0 ; i < p_Running.size(); i++ ) { p_Running[i]->set_alert(true); } } class cFunction { bool p_alert; boost::mutex p_lock; public: cFunction(); void set_alert(const bool& alert_); bool get_alert(); void execution(); void alert_clean(); }; void cFunction::execution() { // faire des traitements for (unsigned int i = 0; i < 1000; i++) { if (!get_alert()) { // traitement boucle } } if (!get_alert()) { // traitement singulier } if (get_alert()) { alert_clean(); } } cFunction::cFunction() : p_alert(false) { } void cFunction::set_alert( const bool& alert_ ) { boost::mutex::scoped_lock guard(p_lock); p_alert = alert_; } bool cFunction::get_alert() { boost::mutex::scoped_lock guard(p_lock); return p_alert; } void cFunction::alert_clean() { // opérations pour terminer correctement la fonction set_alert(false); }
Partager