![](/img/trans.png)
[英]Is it possible to ensure a constexpr function is called at most once at compile time?
[英]Ensure one function executes once at a time
我如何確保一個函數被調用一次,並且只有在上一次調用的任務完全執行后才能再次調用它。
我說的是MFC c++ VS2019 windows中的一個事件生成函數調用,所以調用函數是不可控的。
例如
BEGIN_MESSAGE_MAP(CMyWnd2, CWnd)
ON_MESSAGE(WM_MYMESSAGE, OnMyMessage)
END_MESSAGE_MAP()
// inside the class declaration
afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
LRESULT CMyWnd2::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
// The task here might take some time to finish execution.
return 0;
}
我想確保在執行OnMyMessage(WPARAM wParam, LPARAM lParam)
,必須忽略對此函數的任何其他調用。
這是一個有點不清楚如何OnMyMessage
可以當你還在被稱為OnMyMessage
。
但無論如何這可能會OnMyMessage
幫助:它不會阻止OnMyMessage
的調用,但是一旦在OnMyMessage
我們只檢查是否有另一個正在進行的OnMyMessage
,在這種情況下我們什么都不做。
LRESULT CMyWnd2::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
static bool inMyMessage;
if (inMyMessage)
return 0;
inMyMessage = true;
// The task here might take some time to finish execution.
inMyMessage = false;
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.