gcc (GCC) 4.6.3
valgrind-3.6.1
送受信用の2つの異なるスレッドでいくつかのメッセージを送受信するアプリケーションを作成しました。ロックにpthread、条件変数、ミューテックスを使用します。
ただし、送信者はメッセージを送信し、受信者にメッセージを受信して処理するように通知します。これはwhileループで行われます。
ただし、ctrl-cを使用してアプリケーションを終了し、割り込みを処理すると、問題が発生します。送信されているメッセージがない場合、受信者は受信を待機しているwhileループでスタックします。
メインスレッドはjoinを呼び出し、レシーバーが終了するのを待ってブロックします。しかし、それは待っているほどではありませんpthread_cond_wait
。
pthread_cancel
またはを使用することを考えていましたpthread_kill
。しかし、それはスレッドが正常に終了することを許可しないので、私はそれをするのが好きではありません。
提案をありがとう。
主な機能
void main(void)
{
/* Do some stuff here */
/* Start thread that will send a message */
if(pthread_create(&thread_recv_id, &thread_attr, thread_recv_fd, NULL) == -1) {
fprintf(stderr, "Failed to create thread, reason [ %s ]",
strerror(errno));
break;
}
printf("Start listening for receiving data'\n");
/* Start thread to receive messages */
if(pthread_create(&thread_send_id, &thread_attr, thread_send_fd, NULL) == -1) {
fprintf(stderr, "Failed to create thread for receiving, reason [ %s ]",
strerror(errno));
break;
}
/* Clean up threading properties */
pthread_join(thread_send_id, NULL);
pthread_join(thread_recv_id, NULL); <---- blocking here waiting for the recv thread to finish
pthread_mutex_destroy(&mutex_queue);
pthread_cond_destroy(&cond_queue);
return 0;
}
送信者スレッド
void *thread_send_fd()
{
pthread_mutex_lock(&mutex_queue);
if(send_fd((int)fd) == FALSE) {
/* Just continue to send another item */
continue;
}
/* Signal the waiting thread to remove the item that has been sent */
pthread_cond_signal(&cond_queue);
pthread_mutex_unlock(&mutex_queue);
}
レシーバースレッド
void *thread_recv_fd()
{
while(is_receiving()) {
pthread_mutex_lock(&mutex_queue);
/* Wait for an item to be sent on the queue */
pthread_cond_wait(&cond_queue, &mutex_queue); <---- waiting here
queue_remove();
pthread_mutex_unlock(&mutex_queue);
}
pthread_exit(NULL);
}