shaggy
shaggy

Reputation: 651

Running two threads [c]

I need to run two separate processes simultaneously. One is just listening on port X, and sends data when it receives sth, the second one is doing some other stuff.

Ive tried this: pthread_t thread1, thread2; int iret1, iret2;

iret1 = pthread_create( &thread1, NULL, getAddress(), NULL);

iret2 = pthread_create( &thread2, NULL, operate(), (struct IPlist) *IPlist);

In the first one I would like to run get(Address) - the listening and sending part, in the second one I need to run operate() with one arg: *IP list //(struct IPlist *IPlist)

BUT, it shows errors:

warning: passing argument 3 of ‘pthread_create’ makes pointer from integer without a cast /usr/include/pthread.h:225: note: expected ‘void * (*)(void *)’ but argument is of type int

error: incompatible type for argument 4 of ‘pthread_create’ /usr/include/pthread.h:225: note: expected ‘void * __restrict__’ but argument is of type ‘struct IPlist’

What is wrong here?

I do not really understand the manual, so I am asking here.

Thanks for any help!!

Upvotes: 2

Views: 465

Answers (2)

Tugrul Ates
Tugrul Ates

Reputation: 9687

Use getAddress and operate instead of getAddress() and operate() when calling pthread_create. You have to supply functions instead of their return values.

You also need to supply your data to these threads through the last argument. It should go like this:

struct IPlist *IPlist;
iret1 = pthread_create( &thread1, NULL, getAddress, IPlist);
iret2 = pthread_create( &thread2, NULL, operate, IPlist);

And your functions should be like:

void* getAddress(void* data) { struct IPlist *IPlist = data ; /* ... */ }
void* operate(void* data) { struct IPlist *IPlist = data ; /* ... */ }

If your program keeps stalling, be sure to check out pthread_mutex objects.

Upvotes: 5

maverick
maverick

Reputation: 19

I think you should pass address of function.

struct IPlist *IPlist;
iret1 = pthread_create( &thread1, NULL, (void *) &getAddress, (void *)IPlist);
iret2 = pthread_create( &thread2, NULL, (void *) &operate, (void *)IPlist);

assuming that function decleration is 

 - void getAddress (void *)

 and 

 - void operate (void *)

.

Upvotes: 1

Related Questions