-2

I have a program in which we can monitor 2 objects at same time.

myThread = new thread (thred1, id);
vec.push_back (myThread);

In thred1 function,i use Boolean function to read the stored values from a different vector and it runs parallely like this: element found 2 -- hj
HUMIDITY-1681692777 DISPLAYED IN RH
element found 1 -- hj
TEMPERATURE--1714636915 IN DEGREE CELSIUS

This keeps on running as that is what my program should do. I have a case where I need to get ID from the user and stop that particular thread and the other should keep running till I stop it.Can someone help me with that?

void thred1 (int id) 
{
bool err = false;
    while (stopThread == false)
    {
    for (size_t i = 0; i < v.size (); i++)
        {
            if (id == v[i]->id)
                {
                cout << "element found " << v[i]->id << " -- " << v[i]->name << endl;
                v[i]->Read ();
                this_thread::sleep_for (chrono::seconds (4));
                err = true;
                break;
                }
        }

        if (!err)
        {
        cout << "element not found" << endl;
       break;
        }
    }
}
inihsrah
  • 11
  • 8
  • Could you explain this in more detail? It may just be me but I'm not entirely sure what it is you're asking. – Pickle Rick Nov 11 '19 at 06:17
  • What type is `stopThread`? If it's not an atomic then your code is wrong: [SO: Multithreading program stuck in optimized mode but runs normally in -O0](https://stackoverflow.com/a/58516119/7478597) – Scheff's Cat Nov 11 '19 at 06:35
  • I have a program in which I need to add,display,start and stop monitor the values.to add function I use id and name and store it in a vector.when I start monitoring a value,it keeps running continuously and simultaneously I can add another components or monitor them.Now I should stop monitoring one component with id entered – inihsrah Nov 11 '19 at 06:35
  • Even if `stopThread` is atomic it may need upto 4 seconds until thread stops (due to `this_thread::sleep_for (chrono::seconds (4));`). If `v[i]->Read ();` is blocking it cannot react on `stopThread` until data can be read. – Scheff's Cat Nov 11 '19 at 06:37
  • stopThread is a Boolean function@Scheff – inihsrah Nov 11 '19 at 06:37
  • Is only one value being monitored at a time or many with each on a separate thread? – Pickle Rick Nov 11 '19 at 06:37
  • 1
    _stopThread is a Boolean function_ ??? You don't call a function - you just check `stopThread`: `while (stopThread == false)` Please, show the declaration of `stopThread`. – Scheff's Cat Nov 11 '19 at 06:38
  • That works fine for my program..but I cannot stop one thread alone.@ScheffI just declared it as false,and when I change it to true,it stops monitoring and no values are read – inihsrah Nov 11 '19 at 06:38
  • many values are being monitored..and I need to stop one value whose Id is entered@PickleRick – inihsrah Nov 11 '19 at 06:40
  • So you have a single thread for each value being monitored, which could be many. The user enters an ID and if any threads exist that are monitoring that value then they're stopped? If that's the case, I can give you a proper answer. – Pickle Rick Nov 11 '19 at 06:41
  • ya..that's the case.I don't know how to stop one particular thread based on id@PickleRick – inihsrah Nov 11 '19 at 06:47
  • Sounds you like you want an ``std::map`` storing all monitor threads. You can then grab the thread via ``thread_map[HandleId]`` and suspend / terminate it from there, along with removing it from the map. – Pickle Rick Nov 11 '19 at 06:52
  • Firstly, when you come across a problem, isolate it. Start a new program only to explore this particular aspect that's causing problems, so you get a [mcve]. With that, you can ask much better questions. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Nov 11 '19 at 08:10

1 Answers1

0

Suspension
1. Assuming you want to suspend the monitor thread but only temporarily (i.e making any changes) then you can just use a mutex. Lock it before accessing the shared vector and unlock it when you're done, ensuring that only one thread can access the data at a time.
2. You can actively suspend the thread using OS support such as SuspendThread and ResumeThread, in the case of Windows, when it's ready.

Termination
1. You could use an event for each monitor thread, name being linked to the ID would work. At each iteration of the monitor check for the termination event, ending the thread if it's active.
2. Pass some variable to each thread, store them in a map with the thread handle being the key, and similar to the previous option just check the value for each iteration.
3. Store all threads in a map with the handle as key, terminating it directly with OS support.

Honestly there are a ton of ways to do this, the best implementation depends on why exactly you want to stop the monitor thread. Any sort of synchronization object like a mutex should be fine if you're reading from one thread and writing from another. Otherwise, just storing all threads with the internal ID as key and the thread as the value should be fine for terminating monitor threads on demand.

Pickle Rick
  • 808
  • 3
  • 6