Jump to content
  • Advertisement
Sign in to follow this  
lonewolff

Passing reference to a thread?

This topic is 584 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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!