2014-12-06 64 views
0

我在C小白++,並有一個類(mystack),有一個在它的方法(推),它可以使用如何在不同的線程中調用同一類的多個對象的方法?

mystack a; 
a.push(); 

現在我已經創造了A類的多個實例調用,併爲他們每個人我想調用push方法,我想知道如何在不同的線程中調用它們,謝謝。


編輯

完整的代碼如下(這是很長,但很簡單明瞭),有類mystack的兩個實例,他們每個人提方法調用的順序,我要讓方法在不同的線程中調用不同的實例,所以我想在一個線程中執行實例stc的push和pop操作,並且在另一個線程中執行stc2中的相同操作,那麼我將如何實現這一點?

#include "stdafx.h" 
#include <string> 
#include <iostream> 
#include <thread> 
#include <vector> 
#include <mutex> 

using namespace std; 

//static recursive_mutex mtx; 

struct mystack 
{ 
    int *p; 
    unsigned int limit, counter; 

public: 
    unsigned int max() 
    { 
     return limit; 
    } 

    ~mystack() 
    { 
     delete p; 
    } 

    mystack(int k) : limit(k), p(0), counter(0) 
    { 
     if (limit > 0) 
     { 
      p = new int[limit]; 
     } 
    } 

    void push(unsigned int k) 
    { 
     if (counter >= limit) 
     { 
      throw 1; 
     } 

     p[counter] = k; 
     counter++; 
    } 

    int pop() 
    { 
     if (counter <= 0) 
     { 
      throw 1; 
     } 

     counter--; 

     return p[counter]; 
    } 
}; 

int main(int, char*[]) 
{ 
    mystack stc(5); 
    try 
    { 
     stc.push(1); 
     stc.push(2); 
     stc.push(3); 
     stc.push(4); 
     stc.push(5); 
     stc.push(6); 
    } 
    catch (int i) 
    { 
     try 
     { 
      cout << "pop out the values" << endl; 
      while (true) 
      { 
       cout << stc.pop() << " "; 
      } 
     } 
     catch (int j) 
     { 
      cout << endl; 
      cout << "stack is now empty" << endl; 
      cout << endl; 
     } 
    } 


    mystack stc2(3); 
    try 
    { 
     stc2.push(1); 
     stc2.push(2); 
     stc2.push(3); 
     stc2.push(4); 
     stc2.push(5); 
     stc2.push(6); 
    } 
    catch (int i) 
    { 
     try 
     { 
      cout << "pop out the values" << endl; 
      while (true) 
      { 
       cout << stc2.pop() << " "; 
      } 
     } 
     catch (int j) 
     { 
      cout << endl; 
      cout << "stack is now empty" << endl; 
      cout << endl; 
     } 
    } 

    return 0; 
} 

回答

1

你問了一個相當普遍的問題,但假設你沒有已創建還沒有任何線程,使用C++ 11和std ::線程將是你最好的選擇:

#include <iostream> 
#include <thread> 

struct A 
{ 
    int id; 
    A(int i) : id(i) {} 

    void push() { 
     std::cout << id << " pushin." << std::endl; 
    } 
}; 

A obj_a(0); 
void push_global() {obj_a.push();} 

void push_an_A(A& obj) {obj.push();} 

int main() 
{ 
    A obj_b(1), 
     obj_c(2); 

    // globals (not recommended) 
    std::thread thread_a(push_global); 

    // using lambdas 
    auto push_b = [&obj_b](){ 
     obj_b.push(); 
    }; 

    std::thread thread_b(push_b); 

    // binding 
    std::thread thread_c(std::bind(push_an_A, obj_c)); 

    thread_a.join(); 
    thread_b.join(); 
    thread_c.join(); 
} 

請記住使用您的編譯器的等效-std=c++11-pthread選項。

編輯:爲您更新的代碼,(儘管奇使用異常流量控制對那裏發生的),這是因爲考慮你正在做的操作順序和功能把它們插簡單:

void do_sequence(mystack &stack) 
{ 
    try 
    { 
     stack.push(1); 
     stack.push(2); 
     stack.push(3); 
     stack.push(4); 
     stack.push(5); 
     stack.push(6); 
    } 
    catch (int i) 
    { 
     try 
     { 
      cout << "pop out the values" << endl; 
      while (true) 
      { 
       cout << stack.pop() << " "; 
      } 
     } 
     catch (int j) 
     { 
      cout << endl; 
      cout << "stack is now empty" << endl; 
      cout << endl; 
     } 
    } 
} 

int main(int, char*[]) 
{ 
    mystack stc(5), 
     stc2(3); 

    // note that we need to use std::ref() so that the arguments are correctly 
    // passed by reference, otherwise we get memory corruption 
    std::thread thread_stc(std::bind(do_sequence, std::ref(stc))), 
     thread_stc2(std::bind(do_sequence, std::ref(stc2))); 

    thread_stc.join(); 
    thread_stc2.join(); 

    return 0; 
} 
+0

非常感謝你! – photosynthesis 2014-12-06 04:17:00

相關問題