在C ++中使用对象和类使用守护进程 [英] Using daemon process using objects and class in C++

查看:86
本文介绍了在C ++中使用对象和类使用守护进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我正在研究使用C ++编程的线程。我可以使用这样的函数创建一个守护程序线程,



Sir, I am studying about threading using C++ programming. I can create a daemon threads using a function like this,

void print(std::string name)
{
	std::cout << name;
}
int main()
{
	for (int i = 0; i < 10; i++)
	{
		std::thread t(print, "Name\n");
		t.detach();
	}
	int stay;
	std::cin >> stay;
	return 0;
}





但是,我有一个类,并且使用另一个类调用该类的成员函数(不使用继承)但是通过创建具有私有作为访问类型的对象,以便唯一的类可以使用该对象调用另一个类的函数。



这是我的代码:



But, I am having a class and the member function of the class is called using another class (not using inheritance) but by creating object with private as an access type so that the sole class can call the functions of another class using the object.

This is my code:

class Schedule
{
private:
	std::vector<std::string> name_list;
public:
	void add(std::string name)
	{
		name_list.push_back(name);
	}
};

class Add
{
private:
	Schedule obj;
public:
	void push()
	{
		for (int i = 0; i < 10; i++)
		{
			// I dont know how to make this as a daemon process
			obj.add("NAME");
		}
	}
};





我不知道如何将函数添加为守护进程。



我尝试了什么:



我试过提到这个问题,

c ++ - 使用成员函数启动线程 - Stack Overflow [ ^ ]



没有找到任何成功!



我跟进了本教程, thread - C ++参考 [ ^ ]

但找不到我想要的东西。



我不知道该怎么办所以我来到这里请帮助我先生!

任何甚至参考都可以帮助我学习ning this。



谢谢你的时间



I dont know how to make the function add as a daemon process.

What I have tried:

I have tried referring this question,
c++ - Start thread with member function - Stack Overflow[^]

Didn't find any successes on that!

I followed up this tutorial, thread - C++ Reference[^]
but not found what I wanted.

I don't know what to do so I came here kindly help me sir!
Anything even reference could help me in learning this.

Thank you for your time

推荐答案

你的例子毫无意义。线程用于冗长的操作或在等待事件时暂停。但是你想在一个循环内多次执行一个简短的操作(矢量push_back)。



你的问题的答案很简单:

Your example makes no sense. Threads are used for lengthy operations or to suspend when waiting for events. But you want to perform a short operation (vector push_back) multiple times inside a loop.

The answer to your question is quite simple:
// obj.add("NAME");
std::thread th(&Schedule::add, &obj, "NAME");
th.detach();



但这需要在你的 add()函数中锁定 mut std :: mutex 班级成员:


But this would require locking within your add() function where mut is a std::mutex class member:

void add(std::string name)
{
    std::unique_lock<std::mutex> lk(mut);
    name_list.push_back(name);
}



所以更好的解决方案是在Schedule类中添加一个函数,创建一个执行操作的线程:


So a better solution would be adding a function to your Schedule class that creates a thread which performs the operation:

void add_threaded(std::string name)
{
    std::thread th(&Schedule::add, this, name);
    th.detach();
}



但是这些解决方案可能比非线程代码消耗更多时间。



上面的示例使用非静态成员函数创建线程。对于那些 std :: thread 构造函数,需要成员函数的地址和指向类的指针,后跟可选参数。



Surprinsingly没有那么多 std :: thread 教程,涵盖了Web中的类成员函数。所以找到一个或多个有用的东西应该不难。

一些例子:

在C ++中的多线程读取第1部分:启动线程 [ ^ ]

C ++ 11:std :: threads managed由指定的班级RafałCieślak的博客 [ ^ ]

这是CP的一个全新的,它涵盖了可能更适合您的消息处理(在中发出信号)安排类添加项目):

带有消息队列和计时器的C ++ std :: thread事件循环 [ ^ ]


But these solutions will probably consume more time than the not threaded code.

The above examples create threads with a non-static member function. For those the std::thread constructor requires the address of the member function and a pointer to the class followed by optional arguments.

Surprinsingly there are not so many std::thread tutorials covering class member functions in the web as expected. So it should bee not too difficult to find one or more useful ones.
Some examples:
Multithreading in C++0x part 1: Starting Threads[^]
C++11: std::threads managed by a designated class | Rafał Cieślak's blog[^]
A brand new one here at CP that covers message handling which may be more appropriate for you (signaling a thread in the Schedule class to add items):
C++ std::thread Event Loop with Message Queue and Timer[^]


这篇关于在C ++中使用对象和类使用守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆