for_developers:thread_safety
                Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| for_developers:thread_safety [2019/09/10 11:47] – created kunz | for_developers:thread_safety [2024/06/11 09:15] (current) – [Thread Safety] ursgraf | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| Concurrency can not be tested by Google Test framework. Therefore, test executables are needed which can be executed manually or via bash scripts. | Concurrency can not be tested by Google Test framework. Therefore, test executables are needed which can be executed manually or via bash scripts. | ||
| - | To provide an example, a test program testing the Gain block is described | + | To provide an example, a test program testing the Gain block is described | 
| + | |||
| + | ===== The problem ===== | ||
| + | |||
| + | The following code snipped and image show the issue simulated in the test program. The main thread calls **g1.run()** on the gain object. This method is doing a long calculation. Thread t2 is unfortunately changing the gain by calling **g1.setGain(gM2)** during the main threads calculation. This will finally lead to an unexpected result. | ||
| + | |||
| + | |||
| + | < | ||
| + | Matrix< | ||
| + | Matrix< | ||
| + | |||
| + | Gain< | ||
| + | Constant< | ||
| + | |||
| + | std::thread t2{[&] { | ||
| + | g1.setGain(gM2); | ||
| + | }}; | ||
| + | |||
| + | g1.run(); | ||
| + | |||
| + | Matrix< | ||
| + | |||
| + | // Note: The above code is simplified for a better understanding. | ||
| + | </ | ||
| + | |||
| + | |||
| + | {{: | ||
| + | |||
| + | |||
| + | ===== The solution ===== | ||
| + | |||
| + | The Gain block is now implemented thread safe by using the class [[https:// | ||
| + | |||
| + | < | ||
| + | virtual void setGain(Tgain c) { | ||
| + | std:: | ||
| + | gain = c; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | The test program will not show an unexpected result anymore. To see the unexpected result, the locking mechanism must be removed (//comment std:: | ||
for_developers/thread_safety.1568108827.txt.gz · Last modified: 2019/09/10 11:47 by kunz
                
                