snd_pcm_drain stucked.
alsa-project/alsa-lib issue #457 was opened from yangyagami:
When snd_pcm_drain is called, if snd_pcm_drop is called in another thread, snd_pcm_drain will get stuck.
```cpp
#include <iostream>
#include <cmath>
#include <thread>
#include <alsa/asoundlib.h>
#define SAMPLE_RATE 44100
#define FREQUENCY 440.0 // A4 note
#define DURATION 5.0 // seconds
using namespace std::chrono_literals;
int main() {
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int rate = SAMPLE_RATE;
int dir;
int pcm;
pcm = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (pcm < 0) {
std::cerr << "Unable to open PCM device: " << snd_strerror(pcm) << std::endl;
return 1;
}
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(handle, params);
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_rate_near(handle, params, &rate, &dir);
snd_pcm_hw_params_set_channels(handle, params, 1);
pcm = snd_pcm_hw_params(handle, params);
if (pcm < 0) {
std::cerr << "Unable to set HW parameters: " << snd_strerror(pcm) << std::endl;
return 1;
}
int samples = static_cast<int>(DURATION * SAMPLE_RATE);
int16_t *buffer = new int16_t[samples];
for (int i = 0; i < samples; ++i) {
buffer[i] = static_cast<int16_t>(32767 * sin(2 * M_PI * FREQUENCY * i / SAMPLE_RATE));
}
pcm = snd_pcm_writei(handle, buffer, samples);
if (pcm < 0) {
std::cerr << "Playback error: " << snd_strerror(pcm) << std::endl;
}
std::thread t([handle](){
std::this_thread::sleep_for(500ms);
snd_pcm_drop(handle);
});
snd_pcm_drain(handle);
t.join();
delete[] buffer;
snd_pcm_close(handle);
return 0;
}
```gdb output
```shell Thread 1 "main" received signal SIGINT, Interrupt. 0x00007ffff71b5e22 in ?? () from /usr/lib/libc.so.6 (gdb) bt #0 0x00007ffff71b5e22 in ?? () from /usr/lib/libc.so.6 #1 0x00007ffff71a9fda in ?? () from /usr/lib/libc.so.6 #2 0x00007ffff71aa64c in ?? () from /usr/lib/libc.so.6 #3 0x00007ffff71acd1e in pthread_cond_wait () from /usr/lib/libc.so.6 #4 0x00007ffff6b9e2b9 in pw_thread_loop_wait () from /usr/lib/libpipewire-0.3.so.0 #5 0x00007ffff7f2ec8f in ?? () from /usr/lib/alsa-lib/libasound_module_pcm_pipewire.so #6 0x00007ffff777c2b8 in ?? () from /usr/lib/libasound.so.2 #7 0x0000555555558d6f in main () at main.cc:116 (gdb) ```
Issue URL : https://github.com/alsa-project/alsa-lib/issues/457
Repository URL: https://github.com/alsa-project/alsa-lib