Thread: Would these two reference arguement functions give an error?

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    28

    Would these two reference arguement functions give an error?

    I wrote a shared library which has several functions. I am getting an unknown error when running my code.

    Two of my functions may be called at the same time because I am running my code through another program. I've posted my code below.

    Would char buffer[] and int *errnoOut cause an error, if the functions happen to update them simultaneously?

    If so, what is the best way to deal with this?

    Code:
    // Send Data
    int sendIPv6J(int sockfd, char buffer[], int *errnoOut) {
      int n;
    
    	  n = send(sockfd, buffer, strlen(buffer) + 1, 0);
    
      if(n == -1) {
    
    		      *errnoOut = errno;
    
           return(-1);
    
    	   } else {
    
    		      *errnoOut = 0;
    
           return(n);
    
    	   }
    
    }
    
    // END
    
    
    
    // Recv Data
    
    int recvIPv6J(int sockfd, char buffer[], int maxBufferLength, int *errnoOut) {
      int n;
    
    	  n = recv(sockfd, buffer, maxBufferLength, 0);
    
      if(n == -1) {
    
    		    *errnoOut = errno;
    
        return(-1);
    
    	  } else {
    
    		    *errnoOut = 0;
    
        return(n);
    
    	  }
    
    }
    // END
    
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    What does "at the same time" mean?

    If you have two instances of a program running at the same time, there is no conflict at all, since everything occupies a separate address space.

    If you have two threads, then so long as each thread has separate memory for buffer and error number, there is no issue.
    Whilst errno is a global variable, most threading systems localise it to be "thread local" when you're using threads.

    So something like this isn't going to be a problem either.
    Code:
    void *t1 ( void *p ) {
      char buff[100];
      int myErr;
      int res = recvIPv6J(sockfd, buff, sizeof(buff), &myErr);
    }
    
    void *t2 ( void *p ) {
      char buff[100];
      int myErr;
      int res = recvIPv6J(sockfd, buff, sizeof(buff), &myErr);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2017
    Posts
    28
    Thanks.

    That makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this give error?
    By surbhijain93 in forum C Programming
    Replies: 4
    Last Post: 10-04-2014, 03:36 AM
  2. please give me EXAMPLES of FUNCTIONS (Beginners)
    By amplechin in forum C++ Programming
    Replies: 3
    Last Post: 12-01-2011, 06:58 AM
  3. i use pass by reference but my cout give me weird number
    By personal12 in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2011, 03:21 AM
  4. Replies: 7
    Last Post: 08-16-2007, 12:15 PM
  5. why does this give an error?
    By aaronc in forum C Programming
    Replies: 7
    Last Post: 06-18-2004, 08:53 AM

Tags for this Thread