c - is this the right way to initialize a semaphore with two threads -


i new using concept of semaphores. trying integrate sender , receiver single project such if run project both sender , receiver exchange data simultaneously. below tried eclipse cdt ide showing an
**error: ‘receiver’ undeclared (first use in function) pthread_create(mythread2, null, (void*)receiver, null);**

any appreciated.

sem_t semaphore;  void sender() {     while (1) {         sem_wait( & semaphore);         printf("hello sender!\n");         sleep(1); /* not run fast! */         /* write number of messages, re-using existing string-buffer: no leak!!. */         (i = 1; <= 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); /* not run 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 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");                  (i = 0; < 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 amount of time, not consume 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 changed policy when defining 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 publisher chatter application. */     chatpublisher = dds_domainparticipant_create_publisher(participant, pub_qos, null, dds_status_mask_none);     checkhandle(chatpublisher, "dds_domainparticipant_create_publisher");      /* create datawriter chatmessage topic (using 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 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, send %d more messages.", num_msg);      printf("writing message: %s\n", msg - > content);      /* register chat message user (pre-allocating resources it!!) */     userhandle = dds__foodatawriter_register_instance(talker, msg);      /* write message using pre-generated instance handle. */     status = dds__foodatawriter_write(talker, msg, userhandle);     checkstatus(status, "chat_chatmessagedatawriter_write");     /* create subscriber messageboard application. */     chatsubscriber = dds_domainparticipant_create_subscriber(participant, sub_qos, null, dds_status_mask_none);     checkhandle(chatsubscriber, "dds_domainparticipant_create_subscriber");     /* create datareader chatmessagetopic topic (using 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 message messageboard has opened. */     printf("messageboard has opened: send chatmessages \n\n");      /* allocate sequence holders datareader */     msgseq = dds_sequence_chat_chatmessage__alloc();     checkhandle(msgseq, "dds_sequence_chat_namedmessage__alloc");     infoseq = dds_sampleinfoseq__alloc();     checkhandle(infoseq, "dds_sampleinfoseq__alloc");      //initializing 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 thread     printf("starting thread, semaphore 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; } 

it looks missing closing brace '}' @ end of function 'sender'. should address specific error "‘receiver’ undeclared".

[also, "while(0) { ... }" construct in function 'receiver' questionable...]

while answering, please allow me suggest following:

1) issue of locking/synchronization (semaphores, in example) orthogonal dds (or other data communication mechanism). [you have more success if can keep code clean , focused when asking on forum this.]

2) implementations of dds (i can speak coredx dds) thread-safe, , not require protection around api calls. [you may want check specific dds vendor confirm this.] example, difficult me infer if application logic requires locking, doesn't appear does.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -