如何在不同线程中对同一类的多个对象进行方法调用? 我是C++中的NoOB,有一个类(MyStUp),里面有一个方法(PUP),它可以用< /P>调用 mystack a; a.push();
现在我已经创建了多个类A的实例,我想为每个实例调用push方法,我想知道如何在不同的线程中调用它们,谢谢如何在不同线程中对同一类的多个对象进行方法调用? 我是C++中的NoOB,有一个类(MyStUp),里面有一个方法(PUP),它可以用< /P>调用 mystack a; a.push();,c++,multithreading,C++,Multithreading,现在我已经创建了多个类A的实例,我想为每个实例调用push方法,我想知道如何在不同的线程中调用它们,谢谢 编辑 下面是完整的代码(很长,但是非常简单和直接),有两个mystack类的实例,每个实例进行一系列的方法调用,我想在不同的线程中对不同的实例进行方法调用,所以我想在一个线程中对实例stc执行push和pop操作,在另一个线程中对stc2执行相同的操作,我将如何实现这一点 #include "stdafx.h" #include <string> #include <io
编辑 下面是完整的代码(很长,但是非常简单和直接),有两个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;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//静态递归互斥mtx;
结构mystack
{
int*p;
无符号整数限制,计数器;
公众:
无符号int max()
{
退货限额;
}
~mystack()
{
删除p;
}
mystack(intk):极限(k),p(0),计数器(0)
{
如果(限制>0)
{
p=新整数[限值];
}
}
无效推送(无符号整数k)
{
如果(计数器>=限制)
{
投掷1枚;
}
p[计数器]=k;
计数器++;
}
int-pop()
{
如果(counter您问了一个非常一般的问题,但假设您还没有创建任何线程,那么使用C++11和std::thread将是您最好的选择:
#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();
}
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;
}