Commit 4095af5
committed
[compiler-rt] Fix race condition when allocating object
For following program compiled with `-femulated-tls`, there is chance
the first allocated object is different from other allocation.
```
thread_local int id = 0;
std::mutex print_mutex;
int main() {
// std::cout << "thread " << 0 << ": " << &id << std::endl;
std::vector<std::thread> group;
for (int i = 1; i < 8; ++i) {
group.emplace_back([i] {
std::unique_lock<std::mutex> _(print_mutex);
std::cout << "thread " << i << ": " << &id << std::endl;
});
}
for (auto &t : group) t.join();
return 0;
}
Output:
thread 1: 0x7f57040010a8
thread 2: 0x7f56fc000c98
thread 3: 0x7f56fc000c98
thread 5: 0x7f56fc000c98
thread 4: 0x7f56fc000c98
thread 6: 0x7f56fc000c98
thread 7: 0x7f56fc000c98
```1 parent 49331ab commit 4095af5
1 file changed
+6
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
392 | 392 | | |
393 | 393 | | |
394 | 394 | | |
395 | | - | |
396 | | - | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
397 | 401 | | |
398 | 402 | | |
399 | 403 | | |
| |||
0 commit comments