Question about multithreading

Hi


I have a bunch of threads I want to have interacting but I don't know how to access a thread after I initialize

it. I"m coding in C++.


For example:


Clock ck;

std::thread clock_thread(&Clock::loop, &ck);

clock_thread.detach();


How do I get to modify the Clock's thread from say, my Message thread.

That's a very open-ended question. Multithreading is a very large domain.


In general, you don't really want threads to "interact". That will limit your concurrency and defeat the purpose. If you are just trying to run a task in the background and update the UI, that is a different question. There are many ways to solve it. But you are asking from a "C++" perspective. That really isn't appropriate for these Apple forums. An appropriate answer for these forums would involve dispatch queues and blocks, or maybe NSOperations, but that isn't strictly C++.

Well I'm using XCode and C++ so I figured this forum would be appropriate.


This is exactly what I want to do:


I have a MessageManager that loops in a thread.


I have an InputClass that loops in a thread too.


I want to do something simple. I want to input a keypress to the InputClass thread. Then I want the InputClasss to send a message

to the MessageManager alerting it that the key was pressed. Then the MessageManager does some processes....

In this context, Xcode is just an IDE. Coding a text-based interface is not a good, real-world example of using concurrency. It is extremely rare to ever write a production command-line tool with a user interface. It would be even more unusual to do that with concurrency.


That being said, using concurrency to interact with a user interface is a very popular use of concurrency. It is just that the mechanisms of the user interface are so different that it will be difficult to apply anything you learn in a text interface to a real app. Furthermore, any C++ techniques would be particularly difficult to apply on an Apple platform that is focused on Swift and Objective-C.


I think a better approach might be a local college or university. Internet forums, and this forum in particular, when using C++, are awkward.

Not sure to understand the problem.


I want to input a keypress to the InputClass thread. Then I want the InputClasss to send a message to the MessageManager alerting it that the key was pressed. Then the MessageManager does some processes....


Then post a notification from InputClass

And have MessageManager subscribe to this notification with the action to do some process.


Did I miss something ?

I also agree - but perhaps I am missing something.


Don't threads share all global variables?

So one thread can set a variable while the other thread monitors the value of that variable and responds accordingly.

Question about multithreading
 
 
Q