For example, there are shared variables.
int val;
Cls obj;
An atomic bool variable acts as an data indicator.
std::atomic_bool flag = false;
Thread 1 only set these variables.
while (flag == true) { /* Sleep */ }
val = ...;
obj = ...;
flag = true; /* Set flag to true after setting shared variables. */
Thread 2 only get these variables.
while (flag != true) { /* Sleep */ }
int local_val = val;
Cls local_obj = obj;
flag = false; /* Set flag to false after using shared variables. */
My questions are:
- 
For
std::memory_order_seq_cst, which is default forstd::atomic_bool, is it safe to set or get shared variables afterwhile (...) {}? - 
Using
boolinstead ofstd::atomic_boolis correct or not? 
Aucun commentaire:
Enregistrer un commentaire