Click here to Skip to main content
14,732,982 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
typedef std::function < void(const char*,int) > ConsumerCallback;

class first
{
void ConsumeMessage(ConsumerCallback callback);
};

void ConsumeMessage(ConsumerCallback callback)
{ 

.... used ConsumerCallback 

}

class second 
{
void WaitForConsumer(const char* message, const int& length);
void Test();

};
void WaitForConsumer(const char* message, const int& length)
{
  ....
}

void Test()
{

first obj;
boost::thread *startThread= new boost::thread(obj.ConsumeMessage(std::bind(&Test::WaitForConsumer, this, std::placeholders::_1, std::placeholders::_2)));

}

Error:

Error	C2664	'boost::thread::thread(boost::thread &&) noexcept': cannot convert argument 1 from 'void' to 'boost::detail::thread_data_ptr'


What I have tried:

hi,

i have tried 2 classes. class First has ConsumeMessage function using callback.
and another class is second.here i called ConsumeMessage function via boost thread.but could call that function. some error occurred.any another options for using boost thread?.
Posted
Updated 19-Nov-20 17:43pm
Comments
Richard MacCutchan 19-Nov-20 9:13am
   
The error message is clear: you cannot convert nothing to something (The Laws of Physics q.v.).

You are trying to pass the return value of the ConsumeMessage method (that is a void) to a boost::thread constructor.
Have a look at Boost - Thread Management - 1.73.0[^].
You may also consider using std::thread, see std::thread::thread - cppreference.com[^].
   
Comments
Member 14817752 19-Nov-20 9:58am
   
sorry i posted wrongly .

actually i called ConsumeMessage in thread like this.

startThread = new boost::thread(obj.ConsumeMessage(std::bind(&Test::WaitForConsumer, this, std::placeholders::_1, std::placeholders::_2)));
You can use C++11 thread, instead of boost thread because it supports lambda which made life working with thread, a lot simpler. You should separate the callback passing and real work into 2 functions.

std::thread *startThread= new std::thread([&](){

obj.ConsumeMessage(std::bind(&second::WaitForConsumer, this, 
    std::placeholders::_1, std::placeholders::_2));

});
   
Comments
Member 14817752 20-Nov-20 0:14am
   
hi,
thanks for your help.

std::thread *startThread= new std::thread([&](){

obj.ConsumeMessage(std::bind(&second::WaitForConsumer, this, 
    std::placeholders::_1, std::placeholders::_2));

});


i tried above formate it is working fine.

					startThread->join();
					if (!isThreadInterrupt)
						startThread->detach();



i have a flag isThreadInterrupt.if anywhere failed, set false. false means i want to interrupt startThread thread.if have used lambda thread,i could not able to interrupt or detach.. have any other options?
Shao Voon Wong 20-Nov-20 0:23am
   
I am not sure how your code is going to work since I did not see the whole code. isThreadInterrupt should be checked inside the lambda. The lambda is the function that the thread calls to do the work.

std::thread *startThread= new std::thread([&](){

while(isThreadInterrupt) // set it to false to stop the while loop
{
    obj.ConsumeMessage(std::bind(&second::WaitForConsumer, this, 
        std::placeholders::_1, std::placeholders::_2));
}
});
Shao Voon Wong 20-Nov-20 1:06am
   
startThread->join(); will only return when the thread has ended. If the thread is still running, the code after join() has no chance of executing. ConsumeMessage() maybe blocking the thread from completing. And where did you set isThreadInterrupt? In WaitForConsumer()?
Member 14817752 20-Nov-20 2:04am
   
typedef std::function < void(const char*,int) > ConsumerCallback;

class first
{
void ConsumeMessage(ConsumerCallback callback);
};

void ConsumeMessage(ConsumerCallback callback)
{ 

.... used ConsumerCallback 

}

class second 
{
boost::thread *start;
bool isThreadInterrupt;
void WaitForConsumer(const char* message, const int& length);
void Test();
void start();
void stop();

};
second()
{ 
isThreadInterrupt = true;
}

void WaitForConsumer(const char* message, const int& length)
{
  start =  new boost::thread(&second::Start, this);
  start->join();
  if(!isThreadInterrupt)
    stop();
}

void start()
{
  if(condition)
   isThreadInterrupt = true;
  else
   isThreadInterrupt = false;  
}

void stop()
{ 
    start->interrupt();
    start->join();
}


void Test()
{

first obj;
std::thread *startThread= new std::thread([&](){

while(isThreadInterrupt)
{
    obj.ConsumeMessage(std::bind(&second::WaitForConsumer, this, 
        std::placeholders::_1, std::placeholders::_2));
}
});
startThread->join();
}


hi,
this is my code sample.
and yes i have set isThreadInterrupt In WaitForConsumer().and another question is if isThreadInterrupt false means will interrupt whole threads.startThread also i want to stop,this thread still running only.
Shao Voon Wong 20-Nov-20 4:31am
   
This is a new question which you should post in another Q&A.

Your code is in a mess. Why did you spawn another thread in WaitForConsumer()? I do not know what you are trying to do. This is the code sample which should work.

typedef std::function < void(const char*,int) > ConsumerCallback;

class first
{
void ConsumeMessage(ConsumerCallback callback);
};

void ConsumeMessage(ConsumerCallback callback)
{ 

.... used ConsumerCallback 

}

class second 
{
boost::thread *start;
bool isThreadInterrupt;
void WaitForConsumer(const char* message, const int& length);
void Test();
void start();
void stop();

};
second()
{ 
isThreadInterrupt = true;
}

void WaitForConsumer(const char* message, const int& length)
{
  start =  new boost::thread(&second::Start, this);
  start->join();
  printf("isThreadInterrupt: %d\n", isThreadInterrupt);
}

void start()
{
  if(condition)
   isThreadInterrupt = true;
  else
   isThreadInterrupt = false;  
}

void stop()
{ 
    // Thread already ended when joined in WaitForConsumer()
	// so cannot call interrupt() and join()
    // start->interrupt();
    // start->join();
}


void Test()
{

first obj;
// inside WaitForConsumer will spawn a thread.
obj.ConsumeMessage(std::bind(&second::WaitForConsumer, this, 
    std::placeholders::_1, std::placeholders::_2));
}
Member 14817752 20-Nov-20 5:19am
   
thanks.
void ConsumeMessage(ConsumerCallback callback)
{ 

for (;;) 
{
}
}


without for(;;) loop it interrupted. if add for(;;) loop it is running. any other way to stop?

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