Skip to content

Commit 81cb80d

Browse files
fkokosinskifabiobaltieri
authored andcommitted
samples/synchronization: fix thread b pinning
This commit adds a brief thread b suspend while the sample sets its affinity mask. If the call to `k_thread_cpu_pin` is being made while the thread is actively running, then we get `-EINVAL` and the affinity mask is left unchanged. Signed-off-by: Filip Kokosinski <[email protected]>
1 parent 39a70be commit 81cb80d

File tree

1 file changed

+9
-0
lines changed
  • samples/synchronization/src

1 file changed

+9
-0
lines changed

samples/synchronization/src/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,16 @@ int main(void)
110110
#if PIN_THREADS
111111
if (arch_num_cpus() > 1) {
112112
k_thread_cpu_pin(&thread_a_data, 0);
113+
114+
/*
115+
* Thread b is a static thread that is spawned immediately. This means that the
116+
* following `k_thread_cpu_pin` call can fail with `-EINVAL` if the thread is
117+
* actively running. Let's suspend the thread and resume it after the affinity mask
118+
* is set.
119+
*/
120+
k_thread_suspend(thread_b);
113121
k_thread_cpu_pin(thread_b, 1);
122+
k_thread_resume(thread_b);
114123
}
115124
#endif
116125

0 commit comments

Comments
 (0)