C++ C++;线程-pthread_创建,pthread_连接

C++ C++;线程-pthread_创建,pthread_连接,c++,multithreading,pthreads,C++,Multithreading,Pthreads,你能告诉我我做错了什么吗?我正在错误地实现pthread_create int-iret1=pthread_创建(&producer,NULL,producer,void*) int-iret2=pthread_create(&consumer1,NULL,consumer,void*) #包括 #包括 #包括 #包括 #包括 #定义空0 #定义填充1 #定义缓冲区大小20 使用名称空间std; //原型 无效生产(); 无效消耗(int); int缓冲区[缓冲区大小]; int main()

你能告诉我我做错了什么吗?我正在错误地实现pthread_create

int-iret1=pthread_创建(&producer,NULL,producer,void*)

int-iret2=pthread_create(&consumer1,NULL,consumer,void*)

#包括
#包括
#包括
#包括
#包括
#定义空0
#定义填充1
#定义缓冲区大小20
使用名称空间std;
//原型
无效生产();
无效消耗(int);
int缓冲区[缓冲区大小];
int main()
{
int iret1=pthread_create(&producer,NULL,producer,NULL);
//连接线程
返回0;
}

NULL
作为第四个参数传递,而不是
void*
(这只是它的类型)

此外,线程函数的类型应为

void * produce(void *)
{...}

函数返回一个void指针并获取一个void指针的参数。

如果没有使用线程例程参数,只需传递
NULL
指针,而不是
void*

pthread_create( &producer, NULL, produce, NULL );
线程例程应该是
void*()(void*)
类型。你的不一样。应该是这样的:

/// My fancy producer thread routine
extern "C" void* produce( void* arg ) {

    // do your thing here

    return 0; // or something if you want the result in pthread_join
}

另外,这不是最好的方法:)

谢谢,但是我仍然得到了从void()到void*()(void*)的无效转换,我得到了它。谢谢我也忘了更改函数原型,忘了用-lpthread编译
/// My fancy producer thread routine
extern "C" void* produce( void* arg ) {

    // do your thing here

    return 0; // or something if you want the result in pthread_join
}