- 论坛徽章:
- 0
|
我写了个音频收发程序,一个线程收,一个线程发,收线程中用的是recvfrom的阻塞方式,当进程收到退出信号时,我会给一个全局变量置位,从而使两个线程退出。
可是现在的问题是:由于recvfrom工作在阻塞方式,因为非阻塞方式会产生一定的延时。当它收不到数时就会一直阻塞在那里,如果我在主进程里使用 pthread_join函数的话,程序就会死在那儿。于是我没用pthread_join等待线程的退出,而是直接让主进程退出。这样又有一个问题,那就是音频设备有时候没有释放,下次调用的时候就显示device busy,没有声音。有没有方法让程序强制释放某设备?
我把问题详细说一下:
main.c
调用audio程序
ret=vfork();
if(ret==0) execl("audio"........);
else m_handle=ret;
结束audio程序
kill(m_handle,SIG_INT);
waitpid(m_handle..........);
audio.c
int main()
{.............
signal(SIGINT,sig_quit);
pthread_create (&recvthread, NULL, &recvproc,NULL); //接收线程
pthread_create (&sendthread, NULL, &sendproc,NULL); //发送线程
pthread_detach(sendthread);
pthread_detach(recvthread);
while(quit_flag==0) ;
if(0==pthread_cancel(sendthread)) printf("sendthread canceled...\n");
if(0==pthread_cancel(recvthread)) printf("recvthread canceled...\n");
if(-1!=close(fd)) printf("close /dev/dsp success...\n"); //关闭设备
}
void sig_quit(int sig)
{
quit_flag=1;
}
void * sendproc()
{
while(quit_flag!=1)
{
read();//从/dev/dsp中读录音数据
sendto();//发送出去
}
}
void * recvproc()
{
while(quit_flag!=1)
{
recvfrom();//接收数据
write();//写到/dev/dsp中发出声音
}
} |
|