2017-05-29 19 views
0

私は最初のスレッドチュートリアルを書くようになりました。 私は単一スレッドのプロデューサとコンシューマを作成しています。 まだ完了していません。同期が残っています。 しかし、それはコンパイルされていない、私はそのエラーがどのような議論が欠けているか間違っているかを理解していない。 以下は私のコードです。スレッドエラーC2064:termが0引数を取る関数に評価されない

#include<iostream> 
#include<thread> 
#include<sstream> 
#include<list> 
#include<mutex> 
#include<Windows.h> 

using namespace std; 

#define BUCKET_LEN 5 

HANDLE gMutex = NULL; 
HANDLE gSemFull = NULL; 
HANDLE gSemEmpty = NULL; 

class producerConsumer { 
    long i; 
    list<wstring> que; 
public: 
    producerConsumer() { 
     i = 0; 
     que.clear(); 
    } 
    void produce(); 
    void consumer(); 
    void startProducerConsumer(); 
}; 

void producerConsumer::produce() { 
    std::wstringstream str; 
    str << "Producer[" <<"]\n"; 
    que.push_back(str.str()); 
} 

void producerConsumer::consumer() { 
    wstring s = que.front(); 
    cout << "Consumer[" << "]"; 
    wcout << " " << s; 
    que.pop_front(); 
} 

void producerConsumer::startProducerConsumer() { 
    std::thread t1(&producerConsumer::produce); 
    std::thread t2(&producerConsumer::consumer); 
    t1.joinable() ? t1.join() : 1; 
    t2.joinable() ? t2.join() : 1; 
} 

int main() 
{ 
    gMutex = CreateMutex(NULL, FALSE, NULL); 
    if (NULL == gMutex) { 
     cout << "Failed to create mutex\n"; 
    } 
    else { 
     cout << "Created Mutex\n"; 
    } 

    producerConsumer p; 
    p.startProducerConsumer(); 

    if (ReleaseMutex(gMutex)) { 
     cout << "Failed to release mutex\n"; 
    } 
    else { 
     cout << "Relested Mutex\n"; 
    } 
    gMutex = NULL; 
    system("pause"); 
    return 0; 
} 
+0

なぜCreateMutexを使用しますか? –

答えて

3

非静的メンバ関数に呼び出されるオブジェクトを必要とします。このオブジェクトは、std::threadコンストラクタの2番目の引数として渡すことができます(

)。
std::thread t1(&producerConsumer::produce, this); 
std::thread t2(&producerConsumer::consumer, this); 
+0

私は静的ではないことを意味しているので、もっと役に立つと思います。 – Dipak

+1

@Dipak "静的メンバー関数"は、 'struct Foo {static void static_member_function(){}のように' static'キーワードで宣言されたものです。 }}; '。非静的メンバ関数は 'static'キーワードのないメンバ関数です。静的メンバー関数は、特定のオブジェクトではなく、クラス/構造体に存在するため、* class functions *とも呼ばれます。したがって、 'this'ポインタはありません。 [任意の良い初心者の本](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)あなたにこれを語ったでしょう。 –

関連する問題