Article 552NC unistd.h pause() not returning on sigint

unistd.h pause() not returning on sigint

by
SoftSprocket
from LinuxQuestions.org on (#552NC)
The manpage for pause says:
Quote:
pause() causes the calling process (or thread) to sleep until a signal
is delivered that either terminates the process or causes the invoca
tion of a signal-catching function.
portaudio.h indicates that for every Pa_Initialize there must be a Pa_Terminate or else, so I thought wrap it so a destructor calls terminate and then use pause to call exit on sigint. The destructor works fine under normal conditions but the idea here is that if I've put the code into a endless loop SIGINT should trigger an exit and allow for cleanup.

The issue with the code below is that pause does not return and it seems to me it should. Does anyone have some insight into this?

Code:#include <portaudio.h>
#include <iostream>
#include <cstdlib>
#include <signal.h>
#include <unistd.h>
#include <atomic>
#include <cstring>
#include <thread>

class PortAudio {
bool m_initialized;
PaError m_error;
public:
static std::atomic<bool> interrupted;

static void interrupt_signaled (int) {
PortAudio::interrupted.store (true);
}

PortAudio () : m_initialized (false) {
m_error = Pa_Initialize ();

if(m_error == paNoError) {
m_initialized = true;
}

}

virtual ~PortAudio () {
if (m_initialized) {
m_error = Pa_Terminate ();
if (m_error != paNoError) {
std::cerr << "Failed to teminate portaudio: " << Pa_GetErrorText (m_error) << std::endl;
}
}

std::cout << "Portaudio terminated" << std::endl;

}

bool error () {
return m_error != paNoError;
}

PaError errorCode () {
return m_error;
}

void printLastError () {
std::cerr << Pa_GetErrorText (m_error) << std::endl;
}

};

std::atomic<bool> PortAudio::interrupted (false);

void wait_for_signal () {
pause ();

exit (EXIT_SUCCESS);
}

int main () {
std::cout << Pa_GetVersionText () << "\n";

struct sigaction sa;
memset (&sa, 0, sizeof (sa));
sa.sa_handler = PortAudio::interrupt_signaled;
sigfillset (&sa.sa_mask);
sigaction (SIGINT, &sa, NULL);

PortAudio portAudio;

if(portAudio.error()) {
portAudio.printLastError();
exit (EXIT_FAILURE);
}

std::cout << "Portaudio initialized\n";

std::thread wait_thread (wait_for_signal);

wait_thread.join ();

return EXIT_SUCCESS;
}g++ -std=c++14 audio_devices.cpp -o audio_devices -lportaudio -pthreadlatest?d=yIl2AUoC8zA latest?i=ihmxJVck3lA:JF9ySuKxr5E:F7zBnMy latest?i=ihmxJVck3lA:JF9ySuKxr5E:V_sGLiP latest?d=qj6IDK7rITs latest?i=ihmxJVck3lA:JF9ySuKxr5E:gIN9vFwihmxJVck3lA
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments