c++ - how to make method calls of multiple objects of same class in different thread? -
i'm noob in c++ , have class (mystack), there's method (push) in can called using
mystack a; a.push();
now have created multiple instances of class , each of them want call push method, i'm wondering how can call them in different threads, thanks.
edit
the full code below (it's long quite simple , straightforward), there 2 instances of class mystack, each of them make sequence of method calls, want make method calls of different instance in different threads, want make push , pop operations of instance stc in thread , same operations in stc2 in thread, how achieve this?
#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 values" << endl; while (true) { cout << stc.pop() << " "; } } catch (int j) { cout << endl; cout << "stack 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 values" << endl; while (true) { cout << stc2.pop() << " "; } } catch (int j) { cout << endl; cout << "stack empty" << endl; cout << endl; } } return 0; }
you've asked pretty general question, assuming don't have threads have been created yet, using c++11 , std::thread best bet:
#include <iostream> #include <thread> struct { int id; a(int i) : id(i) {} void push() { std::cout << id << " pushin." << std::endl; } }; obj_a(0); void push_global() {obj_a.push();} void push_an_a(a& obj) {obj.push();} int main() { 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(); }
remember use compiler's equivalent of -std=c++11
, -pthread
options.
edit: updated code, (despite odd use of exceptions flow control going on there) it's simple taking sequence of operations you're doing , sticking them in function:
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 values" << endl; while (true) { cout << stack.pop() << " "; } } catch (int j) { cout << endl; cout << "stack empty" << endl; cout << endl; } } } int main(int, char*[]) { mystack stc(5), stc2(3); // note need use std::ref() arguments correctly // passed reference, otherwise 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; }
Comments
Post a Comment