运行两个线程[E]

时间:2011-07-19 10:19:40

标签: c multithreading

我需要同时运行两个独立的进程。一个是侦听端口X,在收到数据时发送数据,第二个是在做其他事情。

我试过这个: pthread_t thread1, thread2; int iret1, iret2;

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

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

在第一个我希望运行get(Address) - 收听和发送部分,在第二个我需要运行operate()并使用一个arg:*IP list //(struct IPlist *IPlist)

但它显示错误:

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’

这里有什么问题?

我真的不懂手册,所以我在这里问。

感谢您的帮助!!

2 个答案:

答案 0 :(得分:5)

在致电getAddress时,请使用operategetAddress()代替operate()pthread_create。您必须提供functions而不是其返回值。

您还需要通过最后一个参数将数据提供给这些线程。它应该是这样的:

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

你的功能应该是:

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

如果您的程序停滞不前,请务必查看pthread_mutex个对象。

答案 1 :(得分:1)

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 *)