[英]C++ Threads - pthread_create, pthread_join
Can you tell me what I am doing wrong here? 你能告诉我我在做什么错吗? I am implementing pthread_create incorrectly 我执行pthread_create错误
int iret1 = pthread_create(&producer, NULL, produce, void*);
int iret2 = pthread_create(&consumer1, NULL, consume, void*);
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <ctime>
#include <time.h>
#define EMPTY 0
#define FILLED 1
#define BUFFER_SIZE 20
using namespace std;
//prototypes
void produce();
void consume(int);
int buffer[BUFFER_SIZE];
int main()
{
int iret1 = pthread_create(&producer, NULL, produce, NULL);
//join the threads
return 0;
}
If you are not using thread routine argument, just pass NULL
pointer instead of void*
: 如果不使用线程例程参数,则只需传递NULL
指针而不是void*
:
pthread_create( &producer, NULL, produce, NULL );
The thread routine is supposed to be of void* ()( void* )
type. 线程例程应该是void* ()( void* )
类型的。 Yours are different. 你不一样 It should be something like: 应该是这样的:
/// 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
}
Also, sleep(3)
is not the greatest way of thread synchronization :) 另外, sleep(3)
并不是线程同步的最大方法:)
Pass NULL
as the fourth parameters, not void *
(that's just its type). 传递NULL
作为第四个参数,而不是void *
(这只是它的类型)。
Also, the type of the thread functions should be 另外,线程函数的类型应为
void * produce(void *)
{...}
The are functions returning a void pointer and taking a parameter of a void pointer. are函数返回void指针并采用void指针的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.