在是乙個簡易聊天中提到,乙個程序只能做一件事,其實不然,只是當時沒有涉及到執行緒的概念。所以今天在用多執行緒來實現一次。
依舊是兩個程式,每個程式包含兩個執行緒。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define msgkey 1234 //鍵值
pthread_t tid[2] = ;
struct msgbuf
;/* 傳送訊息 */
void *send(void *arg)
if (!strncmp(buf.mtext, "bye", 3)) //以bye結束
break;
}
}
}/* 接收訊息 */
void *receive(void *arg)
if (!strncmp(buf.mtext, "bye", 3))
printf("receive : %s\n", buf.mtext);
}
}int main()
ret = pthread_create(&tid[1], null, send, (void *)&msgid); //建立執行緒
if (0 != ret)
ret = pthread_create(&tid[0], null, receive, (void *)&msgid);
if (0 != ret)
pthread_join(tid[0], null); //執行緒等待
pthread_join(tid[1], null);
msgctl(msgid, ipc_rmid, null); //銷毀訊息佇列
return
0;}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define msgkey 1234 //鍵值
pthread_t tid[2] = ;
struct msgbuf
;void *send(void *arg)
if (!strncmp(buf.mtext, "bye", 3)) //以bye結束
if (!strncmp(buf.mtext, "bye", 3))
break;
}}
}
}void *receive(void *arg)
if (!strncmp(buf.mtext, "bye", 3))
printf("receive : %s\n", buf.mtext);
}
}int main()
ret = pthread_create(&tid[1], null, send, (void *)&msgid);
if (0 != ret)
ret = pthread_create(&tid[0], null, receive, (void *)&msgid);
if (0 != ret)
pthread_join(tid[0], null);
pthread_join(tid[1], null);
return
0;}
個人感覺比使用程序來實現清爽很多,哈哈哈 java多執行緒訊息佇列的實現
1 定義乙個佇列快取池 private static list queuecache new linkedlist 2 定義佇列緩衝池最大訊息數,如果達到該值,那麼佇列檢入將等待檢出低於該值時繼續進行。private integer offermaxqueue 2000 3 定義檢出執行緒,如果佇列...
java多執行緒 訊息佇列的實現
1 定義乙個佇列快取池 private static list queuecache new linkedlist 2 定義佇列緩衝池最大訊息數,如果達到該值,那麼佇列檢入將等待檢出低於該值時繼續進行。private integer offermaxqueue 2000 3 定義檢出執行緒,如果佇列...
java多執行緒 訊息佇列
1 定義乙個佇列快取池 private static list queuecache new linkedlist 2 定義佇列緩衝池最大訊息數,如果達到該值,那麼佇列檢入將等待檢出低於該值時繼續進行。private integer offermaxqueue 2000 3 定義檢出執行緒,如果佇列...