Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通知线程变量(信号?)的变化_C++_C_Multithreading_Pthreads - Fatal编程技术网

C++ 通知线程变量(信号?)的变化

C++ 通知线程变量(信号?)的变化,c++,c,multithreading,pthreads,C++,C,Multithreading,Pthreads,我在同一个程序中有main()和thread 有一个名为“status”的变量,可以获取多个值 我需要在变量更改时通知线程(线程cnat等待状态变量,它已经在执行fluent任务) 有没有一个简单的方法?类似于中断?信号怎么样 主机内部的功能: int main() { char *status; ... ... while (1) { switch (status) { case: status1 ...notify the thread case:

我在同一个程序中有main()和thread

有一个名为“status”的变量,可以获取多个值

我需要在变量更改时通知线程(线程cnat等待状态变量,它已经在执行fluent任务)

有没有一个简单的方法?类似于中断?信号怎么样

主机内部的功能:

int main()
{

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

}
如果有人能给我举个例子,那就太好了!
谢谢

这个例子可能对您有所帮助

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
         }
    }
}
DWORD sampleThread(LPVOID参数);
int main()
{
bool-defValue=false;
bool*状态=&defValue;
CreateThread(NULL,0,sampleThread,status,0,NULL);
而(1)
{
//.............
defValue=true;//触发器线程
// ...
}
返回0;
}
DWORD sampleThread(LPVOID参数)
{
bool*syncPtr=reinterpret_cast(参数);
而(1)
{
if(false==*syncPtr)
{
//做点什么
}
else(true=*syncPtr)
{
//做别的事
}
}
}

因为您已经在使用
pthread
库,所以可以使用条件变量告诉线程有数据可以处理。查看更多信息。

我理解您不希望无限期地等待这个通知,但是C++只实现协作调度。你不能只是暂停一个线程,摆弄它的内存,然后恢复它

因此,您必须了解的第一件事是,必须处理您想要发送的信号/操作的线程必须愿意这样做;换句话说,这意味着必须在某个点明确检查信号

线程检查信号的方法有多种:

  • 条件变量:它们需要等待信号(这可能是不需要的),但这种等待可以由持续时间限制
  • 动作队列(又名通道):创建一个信号/动作队列,目标线程每隔一段时间检查一次要做的事情;如果没有什么,它只是继续做它必须做的事情,如果有什么事情你必须决定它是应该做所有事情还是只处理N个第一。当心排队的人太多
  • 只要经常直接检查
    status
    变量,它不会告诉您它更改了多少次(除非它保留了一个历史记录:但是我们回到了队列),但它允许您修改您的方式

考虑到您的要求,我认为队列可能是这三种方法中最好的。

一次或多次?
nofify thread
?表示通知线程在更新的
状态
值时处理?不要在switch语句中使用
char*
。次数。。不管有多少。。我需要处理“status”变量的更改,每个状态都是我需要执行的不同操作。让线程检查status的值,或者你是说回调函数?谢谢。。但是我不能在线程内使用while(1),因为线程正忙于将数据包传输到另一台计算机。我需要通知某人我的意思是在
while..loop
的同时,你的线程将pkt传输到另一个系统,当变量从
main
更改时,它会从main发出通知。因此,这应该要求正确吗?我正在使用视频库,它具有传输视频的功能,我无法更改此函数…@jayeshbhoi-使用
,而
循环会自找麻烦,因为线程只会旋转,无所事事地消耗CPU时间。相反,您需要将线程置于睡眠状态,直到它需要在有事情要处理时唤醒。@Sean ohh…我明白了,线程将在状态变量从main right更改时唤醒。有关此要求,请参阅linkCondition变量确实需要等待它们,但OP不希望。。。