2010-12-01 16 views
2

ここで間違っていることを教えてもらえますか?私は間違って第四パラメータとしてC++スレッド - pthread_create、pthread_join

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; 
} 

答えて

4

、ちょうどvoid*の代わりにNULLポインタを渡す:

pthread_create(&producer, NULL, produce, NULL); 

スレッドルーチンはvoid*()(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 
} 

また、sleep(3)thread synchronization :)

+0

私はそれを手に入れました。ありがとう。私は関数プロトタイプを変更して-lpthreadを使ってコンパイルするのを忘れていました – Tony

1

パスNULL、ないvoid *(そのちょうどそのタイプ)のpthread_createを実施しています。

また、スレッド関数の種類は

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

ザがvoidポインタを返すとボイドポインタのパラメータを取る関数であるべきです。あなたがスレッドルーチンの引数を使用していない場合

+0

感謝の最大の方法ではありませんが、私はまだ無効(*)(から無効な変換を得る):のようなものでなければなりませんvoid *) – Tony

関連する問題