3

I have main() and thread in the same program.

there is a variable named "status", that can get several values

I need that when the variable changes, to notify the thread (the thread cnat wait for the status variable, it is already doing fluent task) .

is there an easy way to do so? similar to interrupts? how about signals?

the function inside the main:

int main()
{

 char *status;
 ... 
 ...
 while (1)
 {
 switch (status)
   {
     case: status1 ...notify the thread
     case: status2 ...notify the thread
     case: status3 ...notify the thread
   }
 }

}

if someone could give me an example it will be great! thanks!

user1673206
  • 1,671
  • 1
  • 23
  • 43

3 Answers3

2

Since you're already using the pthread library you can use conditional variables to tell the thread that there is data ready for processing. Take a look at this StackOverflow question for more information.

Community
  • 1
  • 1
Sean
  • 60,939
  • 11
  • 97
  • 136
2

I understand that you do not want to wait indefinitely for this notification, however C++ only implements cooperative scheduling. You cannot just pause a thread, fiddle with its memory, and resume it.

Therefore, the first thing you have to understand is that the thread which has to process the signal/action you want to send must be willing to do so; which in other words means must explicitly check for the signal at some point.

There are multiple ways for a thread to check for a signal:

  • condition variable: they require waiting for the signal (which might be undesirable) but that wait can be bounded by a duration
  • action queue (aka channel): you create a queue of signals/actions and every so often the target thread checks for something to do; if there is nothing it just goes on doing whatever it has to do, if there is something you have to decide whether it should do everything or only process the N firsts. Beware of overflowing the queue.
  • just check the status variable directly every so often, it does not tell you how many times it changed (unless it keeps an history: but then we are back to the queue), but it allows you to amend your ways.

Given your requirements, I would think that the queue is probably the best idea among those three.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
1

Might be this example helpful for you.

DWORD sampleThread( LPVOID argument );

int main()
{
    bool defValue = false;
    bool* status = &defValue;

    CreateThread(NULL, 0,   sampleThread,  status,  0,NULL);                             

     while(1)
    {
        //.............
        defValue = true; //trigger thread
        // ...
    }

    return 0;
}

DWORD sampleThread( LPVOID argument )
{
    bool* syncPtr = reinterpret_cast<bool*>(argument); 
    while (1)
    {
        if (false == *syncPtr)
        {
            // do something
        }
        else (true = *syncPtr)
        {
            //do somthing else
         }
    }
}
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • thanks.. but I cant use while (1) inside the thread because the thread is busy in transmitting packets to another computer. I need to notify somehow – user1673206 Feb 01 '14 at 11:09
  • i mean in `while..loop` in mean times your thread transimte pkt to another system and it notified from main when variable changed from `main`.So that is supposed to require right? – Jayesh Bhoi Feb 01 '14 at 11:17
  • im using video library and it has function of transmitting the video, I cant change this function... – user1673206 Feb 01 '14 at 11:19
  • 1
    @jayeshbhoi - Using a `while` loop is asking for trouble as the thread will just spin around eating up CPU time doing nothing. Instead you need to put the thread to sleep until it needs to wake up when there's something to process. – Sean Feb 01 '14 at 11:27
  • @Sean ohh...i see so thread will wake up on change in status variable from main right.And for this requirement please OP refer link – Jayesh Bhoi Feb 01 '14 at 12:07
  • @jayeshbhoi - your link doesn't work, there a `>` at the end of it! – Sean Feb 01 '14 at 12:11