Sign in to follow this  

Passing reference to a thread?

This topic is 369 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys,

I am trying to pass a reference to a variable to a thread to use as a semaphore along with mutex. But I get this error;
 

Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'


This is my code;
 
// The thread code
void task1(bool& semaphore)
{
while (true)
{
if (semaphore)
{
std::cout << "Test\r\n";
mtx.lock();
semaphore = false;
mtx.unlock();
}
}
}
// Main()
int main()
{
bool handshake = false;

std::thread t1(task1, &handshake);

while (true)
{
mtx.lock();
handshake = true;
mtx.unlock();
system("pause");
}

return 0;
}
So in effect the first thread will be continuously running, and every time a key is pressed the thread will display something ("Test").

(I know there is a few things wrong here with threads never being able to 'join' etc.. But I am more worried about passing a reference to the thread function at this stage).


Can this be done with threads or would I have to use a global variable?(which is what I am trying to avoid).

Thanks in advance :) Edited by DarkRonin

Share this post


Link to post
Share on other sites

This topic is 369 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this