我是使用信號量概念的新手。我想要做的是將一個發送者和一個接收者集成到一個項目中,這樣如果我運行這個項目,發送者和接收者就可以同時交換數據。下面是我嘗試過但我的Eclipse CDT IDE顯示
**error: ‘receiver’ undeclared (first use in this function) pthread_create(mythread2, NULL, (void*)receiver, NULL);**
這是用兩個線程初始化信號量的正確方法
任何幫助讚賞。
sem_t semaphore;
void sender() {
while (1) {
sem_wait(& semaphore);
printf("Hello from the sender!\n");
sleep(1); /* do not run so fast! */
/* Write any number of messages, re-using the existing string-buffer: no leak!!. */
for (i = 1; i <= NUM_MSG; i++) {
msg - > index = i;
snprintf(msg - > content, MAX_MSG_LEN, "Message no. %d", msg - > index);
printf("Writing message: %s\n", msg - > content);
status = Chat_ChatMessageDataWriter_write(talker, msg, userHandle);
checkStatus(status, "Chat_ChatMessageDataWriter_write");
sleep(1); /* do not run so fast! */
}
sem_post(& semaphore);
printf("hello before exit\n");
// pthread_exit(NULL);
printf("hello after exit\n");
sleep(1);
}
void receiver() {
while (0) {
sem_wait(& semaphore);
printf("Hello from the receiver!\n");
while (!terminated) {
status = Chat_ChatMessageDataReader_take(
chatAdmin,
msgSeq,
infoSeq,
DDS_LENGTH_UNLIMITED,
DDS_ANY_SAMPLE_STATE,
DDS_ANY_VIEW_STATE,
DDS_ALIVE_INSTANCE_STATE);
checkStatus(status, "Chat_NamedMessageDataReader_take");
for (i = 0; i < msgSeq - > _length; i++) {
Chat_ChatMessage * msg = & (msgSeq - > _buffer[i]);
printf("%s\n", msg - > content);
fflush(stdout);
}
}
sem_post(& semaphore);
status = Chat_ChatMessageDataReader_return_loan(chatAdmin, msgSeq, infoSeq);
checkStatus(status, "Chat_ChatMessageDataReader_return_loan");
/* Sleep for some amount of time, as not to consume too much CPU cycles. */
#ifdef USE_NANOSLEEP
sleeptime.tv_sec = 0;
sleeptime.tv_nsec = 100000000;
nanosleep(& sleeptime, & remtime);
#elif defined _WIN32
Sleep(100);
#else
usleep(1000000);
#endif
}
}
}
int main(void) {
-- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- --
/* Use the changed policy when defining the ChatMessage topic */
chatMessageTopic = DDS_DomainParticipant_create_topic(
participant,
"Chat_ChatMessage",
chatMessageTypeName,
history_topic_qos,
NULL,
DDS_STATUS_MASK_NONE);
checkHandle(chatMessageTopic, "DDS_DomainParticipant_create_topic (ChatMessage)");
/* Create a Publisher for the chatter application. */
chatPublisher = DDS_DomainParticipant_create_publisher(participant, pub_qos, NULL, DDS_STATUS_MASK_NONE);
checkHandle(chatPublisher, "DDS_DomainParticipant_create_publisher");
/* Create a DataWriter for the ChatMessage Topic (using the appropriate QoS). */
talker = DDS_Publisher_create_datawriter(
chatPublisher,
chatMessageTopic,
DDS_DATAWRITER_QOS_USE_TOPIC_QOS,
NULL,
DDS_STATUS_MASK_NONE);
checkHandle(talker, "DDS_Publisher_create_datawriter (chatMessage)");
/* Initialize the chat messages on Heap. */
msg = Chat_ChatMessage__alloc();
checkHandle(msg, "Chat_ChatMessage__alloc");
msg - > userID = ownID;
msg - > index = 0;
msg - > content = DDS_string_alloc(MAX_MSG_LEN);
checkHandle(msg - > content, "DDS_string_alloc");
snprintf(msg - > content, MAX_MSG_LEN, "Hi there, I will send you %d more messages.", NUM_MSG);
printf("Writing message: %s\n", msg - > content);
/* Register a chat message for this user (pre-allocating resources for it!!) */
userHandle = DDS__FooDataWriter_register_instance(talker, msg);
/* Write a message using the pre-generated instance handle. */
status = DDS__FooDataWriter_write(talker, msg, userHandle);
checkStatus(status, "Chat_ChatMessageDataWriter_write");
/* Create a Subscriber for the MessageBoard application. */
chatSubscriber = DDS_DomainParticipant_create_subscriber(participant, sub_qos, NULL, DDS_STATUS_MASK_NONE);
checkHandle(chatSubscriber, "DDS_DomainParticipant_create_subscriber");
/* Create a DataReader for the chatMessageTopic Topic (using the appropriate QoS). */
chatAdmin = DDS_Subscriber_create_datareader(
chatSubscriber,
chatMessageTopic,
DDS_DATAREADER_QOS_USE_TOPIC_QOS,
NULL,
DDS_STATUS_MASK_NONE);
checkHandle(chatAdmin, "DDS_Subscriber_create_datareader");
/* Print a message that the MessageBoard has opened. */
printf("MessageBoard has opened: send ChatMessages \n\n");
/* Allocate the sequence holders for the DataReader */
msgSeq = DDS_sequence_Chat_ChatMessage__alloc();
checkHandle(msgSeq, "DDS_sequence_Chat_NamedMessage__alloc");
infoSeq = DDS_SampleInfoSeq__alloc();
checkHandle(infoSeq, "DDS_SampleInfoSeq__alloc");
//initializing the semaphore
sem_init(& semaphore, 0, 1);
pthread_t * mythread1;
pthread_t * mythread2;
mythread1 = (pthread_t *) malloc(sizeof(* mythread1));
mythread2 = (pthread_t *) malloc(sizeof(* mythread2));
//start the thread
printf("Starting thread, semaphore is unlocked.\n");
pthread_create(mythread1, NULL, (void *) sender, NULL);
pthread_create(mythread2, NULL, (void *) receiver, NULL);
getchar();
sem_wait(& semaphore);
printf("Semaphore locked.\n");
getchar();
printf("Semaphore Unlocked.\n");
sem_post(& semaphore);
getchar();
return 0;
}
您的代碼不能編譯。如果你沒有發佈你的實際代碼,你如何期待我們來幫助你? – EOF
@EOF請看看完整代碼 –
缺少頭文件,仍然無法編譯。 – EOF