diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index e8dd588a5f..61f9b56bae 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -44,6 +44,7 @@ const char EC2_WEST_MIXER[] = "54.241.92.53"; const int AUDIO_UDP_LISTEN_PORT = 55444; int starve_counter = 0; +bool stopAudioReceiveThread = false; StDev stdev; @@ -202,7 +203,7 @@ void *receiveAudioViaUDP(void *args) { delete[] filename; } - while (true) { + while (!stopAudioReceiveThread) { if (sharedAudioData->audioSocket->receive((void *)receivedData, &receivedBytes)) { bool firstSample = (currentReceiveTime.tv_sec == 0); @@ -284,8 +285,6 @@ Audio::Audio(Oscilloscope * s) audioData->audioSocket = new UDPSocket(AUDIO_UDP_LISTEN_PORT); audioData->ringBuffer = new AudioRingBuffer(RING_BUFFER_SIZE_SAMPLES); - pthread_t audioReceiveThread; - AudioRecThreadStruct threadArgs; threadArgs.sharedAudioData = audioData; @@ -435,6 +434,9 @@ bool Audio::terminate () logFile.close(); } + stopAudioReceiveThread = true; + pthread_join(audioReceiveThread, NULL); + return true; error: diff --git a/interface/src/Audio.h b/interface/src/Audio.h index c9f2e55cfc..7f1fa9143f 100644 --- a/interface/src/Audio.h +++ b/interface/src/Audio.h @@ -41,6 +41,9 @@ private: // audio stream handle PaStream *stream; + // audio receive thread + pthread_t audioReceiveThread; + // give access to AudioData class from audioCallback friend int audioCallback (const void*, void*, unsigned long, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void*); }; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 0f96ceb951..235f6be8f0 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -59,6 +59,8 @@ char DOMAIN_IP[100] = ""; // IP Address will be used first if not empty stri const int DOMAINSERVER_PORT = 40102; AgentList agentList; +pthread_t networkReceiveThread; +bool stopAgentDataReceiveThread = false; // For testing, add milliseconds of delay for received UDP packets int packetcount = 0; @@ -360,6 +362,8 @@ void terminate () { //close(serial_fd); audio.terminate(); + stopAgentDataReceiveThread = true; + pthread_join(networkReceiveThread, NULL); exit(EXIT_SUCCESS); } @@ -763,7 +767,7 @@ void *networkReceive(void *args) ssize_t bytesReceived; char *incomingPacket = new char[MAX_PACKET_SIZE]; - while (true) { + while (!stopAgentDataReceiveThread) { if (agentList.getAgentSocket()->receive(&senderAddress, incomingPacket, &bytesReceived)) { packetcount++; bytescount += bytesReceived; @@ -938,7 +942,6 @@ int main(int argc, char** argv) agentList.audioMixerSocketUpdate = &audioMixerUpdate; // create thread for receipt of data via UDP - pthread_t networkReceiveThread; pthread_create(&networkReceiveThread, NULL, networkReceive, NULL); glutInit(&argc, argv); @@ -969,7 +972,6 @@ int main(int argc, char** argv) glutMainLoop(); - pthread_join(networkReceiveThread, NULL); ::terminate(); return EXIT_SUCCESS; }