[alsa-devel] [PATCH] test/pcm-multi-thread: Remove pthread_cancel, add snd_pcm_close
Because 1) Apparently, our thread cancellation policy is "don't even think about it" and we shouldn't break our own rules 2) It is good practice to close pcm after usage
Signed-off-by: David Henningsson diwic@ubuntu.com --- test/pcm-multi-thread.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/test/pcm-multi-thread.c b/test/pcm-multi-thread.c index da1b87c..26de7db 100644 --- a/test/pcm-multi-thread.c +++ b/test/pcm-multi-thread.c @@ -219,18 +219,22 @@ int main(int argc, char **argv) return 1; }
- if (setup_params()) + if (setup_params()) { + snd_pcm_close(pcm); return 1; + }
buf = calloc(1, snd_pcm_format_size(format, bufsize) * channels); if (!buf) { fprintf(stderr, "cannot alloc buffer\n"); + snd_pcm_close(pcm); return 1; }
for (i = 0; i < num_threads; i++) { if (pthread_create(&peeper_threads[i], NULL, peeper, (void *)(long)i)) { fprintf(stderr, "pthread_create error\n"); + snd_pcm_close(pcm); return 1; } } @@ -255,9 +259,8 @@ int main(int argc, char **argv)
running = 0; for (i = 0; i < num_threads; i++) - pthread_cancel(peeper_threads[i]); - for (i = 0; i < num_threads; i++) pthread_join(peeper_threads[i], NULL);
+ snd_pcm_close(pcm); return 1; }
David Henningsson wrote:
- It is good practice to close pcm after usage
[...] for (i = 0; i < num_threads; i++) { if (pthread_create(&peeper_threads[i], NULL, peeper, (void *)(long)i)) { fprintf(stderr, "pthread_create error\n");
} }snd_pcm_close(pcm); return 1;
Any previously created threads are already running. Calling some snd_* function on an already-closed device is worse than letting the OS clean up after the program exits.
If we'd wanted to be strictly correct, we would have to send a message to the threads and wait for them to exit (as the cleanup code below already does).
running = 0;
This variable should be volatile.
- for (i = 0; i < num_threads; i++)
for (i = 0; i < num_threads; i++) pthread_join(peeper_threads[i], NULL);pthread_cancel(peeper_threads[i]);
Regards, Clemens
participants (2)
-
Clemens Ladisch
-
David Henningsson