User Tools

Site Tools


for_developers:thread_safety

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
for_developers:thread_safety [2019/11/01 10:11] – Add link to source code kunzfor_developers:thread_safety [2026/04/12 16:50] (current) – [The solution] 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 below. The full source code is available on GitHub at: https://github.com/eeros-project/eeros-framework/blob/master/examples/development/multithreading.cpp+To provide an example, a test program testing the Gain block is described below. The full source code is available on GitHub at: https://github.com/eeros-project/eeros-framework/blob/master/examples/devel/multithreading.cpp
  
-===== The problem =====+===== 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.   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.  
Line 34: Line 34:
  
  
-===== The solution =====+===== The Solution =====
  
 The Gain block is now implemented thread safe by using the class [[https://en.cppreference.com/w/cpp/thread/lock_guard|lock_guard]] and a [[https://en.cppreference.com/w/cpp/thread/mutex|mutex]]. The code snipped below shows the **setGain()** method as an example. The Gain block is now implemented thread safe by using the class [[https://en.cppreference.com/w/cpp/thread/lock_guard|lock_guard]] and a [[https://en.cppreference.com/w/cpp/thread/mutex|mutex]]. The code snipped below shows the **setGain()** method as an example.
for_developers/thread_safety.1572599515.txt.gz · Last modified: by kunz