Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir, I am studying about threading using C++ programming. I can create a daemon threads using a function like this,

C++
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:
C++
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");
		}
	}
};


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
Posted
Updated 7-Feb-17 0:54am
Comments
Richard MacCutchan 7-Feb-17 3:47am    
Once the process is detached you cannot use any of its objects or methods directly. You need to use some cross process mechanism such as shared memory, IPC, sockets etc.
[no name] 7-Feb-17 3:52am    
so sir, can we create a regular threads if yes, how can w do it could you share any links for reference so that I can learn from it? Thank you
Richard MacCutchan 7-Feb-17 3:57am    
Google for any of those words.
[no name] 7-Feb-17 5:22am    
Sir, I've searched the google and I am not finding what I need. Kindly help me sir. Even the link will help me to learn. Thank you
Richard MacCutchan 7-Feb-17 7:42am    
I don't have any links; that is why I told you to use Google.

1 solution

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();

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);
}

So a better solution would be adding a function to your Schedule class that creates a thread which performs the operation:
C++
void add_threaded(std::string name)
{
    std::thread th(&Schedule::add, this, name);
    th.detach();
}

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[^]
 
Share this answer
 
Comments
[no name] 7-Feb-17 7:47am    
Thank you for your kind solution sir. I have been searching the answer for this in the internet for long time. Yes, my example makes no sense, I just tried those things for learning. Thank you once again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900