[alsa-devel] [PATCH] test/pcm-multi-thread: Remove pthread_cancel, add snd_pcm_close

Clemens Ladisch clemens at ladisch.de
Thu Sep 1 08:37:57 CEST 2016


David Henningsson wrote:
>  2) 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++)
> -		pthread_cancel(peeper_threads[i]);
>  	for (i = 0; i < num_threads; i++)
>  		pthread_join(peeper_threads[i], NULL);


Regards,
Clemens


More information about the Alsa-devel mailing list