11.10.6 Закрепление функции ¶
Иногда желательно закрепить функцию в памяти с помощью функции mlock. Это обычно используется для динамически подключаемых функций в файлах oct или mex, которые содержат некоторую инициализацию, и желательно, чтобы вызов clear не удалял эту инициализацию.
В качестве примера,
function my_function () mlock (); ... endfunction
препятствует удалению my_function из памяти после ее вызова, даже если вызвана функция clear. Можно определить, закреплена ли функция в памяти с помощью mislocked, и разблокировать функцию с помощью munlock, что иллюстрируется следующим кодом.
my_function ();
mislocked ("my_function")
⇒ ans = 1
munlock ("my_function");
mislocked ("my_function")
⇒ ans = 0 Распространенное использование mlock заключается в предотвращении удаления переменных, сохраняемых в памяти, как показано в следующем примере:
function count_calls ()
mlock ();
persistent calls = 0;
printf ("count_calls() has been called %d times\n", ++calls);
endfunction
count_calls ();
-| count_calls() has been called 1 times
clear count_calls
count_calls ();
-| count_calls() has been called 2 times mlock также может использоваться для предотвращения влияния изменений в файле m-файла, например, в внешнем редакторе, на текущую сессию Octave; аналогичный эффект можно получить с помощью функции ignore_function_time_stamp.
-
: mlock
()¶ -
Закрепляют текущую функцию в памяти, чтобы ее нельзя было удалить с помощью
clear.См. также: munlock, mislocked, persistent, clear.
© 1996–2023 The Octave Project Developers
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://docs.octave.org/v9.2.0/Function-Locking.html