pthread problem: incompatibl type used in creation

This is a discussion on pthread problem: incompatibl type used in creation within the C Programming forums, part of the General Programming Boards category; hello, I am writing a basic chat client, but when I try to create the pthreads i get this error ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    pthread problem: incompatibl type used in creation

    hello,
    I am writing a basic chat client, but when I try to create the pthreads i get this error message
    'passing argument 3 of 'pthread_create' from incompatible pointer type' along with 'too many arguments to function 'pthread_create'

    the code I am using is as follows, and I am unsure as why the errors are being thrown

    Code:
    void *threadWrite(int sock,int n, struct sockaddr_in server, struct sockaddr_in from , struct hostent *hp, char *argv[]){
    size_t length;char buffer[256];
    while(1){
    bcopy((char *)hp->h_addr, 
            (char *)&server.sin_addr,
             hp->h_length);
       server.sin_port = htons(atoi(argv[2]));
       length=sizeof(struct sockaddr_in);
       printf("Please enter the message: ");
       bzero(buffer,256);
       fgets(buffer,255,stdin);
       n=sendto(sock,buffer,
                strlen(buffer),0,&server,length);
       if (n < 0) error("Sendto");
    }
    pthread_exit((void*) 27);
    }
    
    void *threadWait(int sock, int n, struct sockaddr_in from){
    size_t length = sizeof(struct sockaddr_in);
    char buffer[256];
    while(1){
    	n = recvfrom(sock,buffer,256,0,&from, &length);
       		if (n < 0) error("recvfrom");
      		// write(1,"Got an ack: ",12);
       		write(1,buffer,n);
    }
    }
    
    void join(int sock,int n,struct sockaddr_in server, struct sockaddr_in from , struct hostent *hp, char *argv[] ){
    size_t length;
    char buffer[256];
    bcopy((char *)hp->h_addr, 
            (char *)&server.sin_addr,
             hp->h_length);
       server.sin_port = htons(atoi(argv[2]));
       length=sizeof(struct sockaddr_in);
       printf("Please enter your nickname: ");
       bzero(buffer,256);
       fgets(buffer,255,stdin);
       n=sendto(sock,buffer,
                strlen(buffer),0,&server,length);
       if (n < 0) error("Sendto");
       n = recvfrom(sock,buffer,256,0,&from, &length);
       if (n < 0) error("recvfrom");
     
    pthread_t waitingThread;
    pthread_t writingThread;
    
    int waitingResult = pthread_create(
    	&waitingThread,
    	NULL,
    	&threadWait,
    	(void*) sock, n,from);
    
    int writingResult = pthread_create(
    	&writingThread,
    	NULL,
    	&threadWrite,
    	(void*) sock, n, server, from, hp, argv);
    
    if(waitingResult !=0){
    	printf("Could not create waiting thread\n");
    	exit(1);
    }
    if(writingResult !=0){
    	printf("Could not create writing thread\n");
    	exit(1);
    }
    }
    I have ommited the networking code, as this works fine and is producing no problems, will include if needed

    many thanks,
    Fiona

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    pthread_create must be used to start a function with the following signature:

    Code:
    void *thread_function(void *);
    You cannot pass more than one argument. If you want to pass more than one argument in, create a struct that contains all the info you want to pass in and give it a pointer to that.

    Have a look at `man pthread_create` (or google that) for details.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 04:23 PM
  2. Compiling Libraries in C
    By TheOriginalGame in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 12:19 PM
  3. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  4. qwerty/azerty keyboard type problem + question about loop.
    By Robin Hood in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 02:03 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21