<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: именные мьютексы, лок и анлок]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=17201&amp;type=atom" />
	<updated>2022-06-22T13:21:01Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=17201</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK: именные мьютексы, лок и анлок]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153635#p153635" />
			<content type="html"><![CDATA[<p>Советую задать вопрос на англоязычном форуме в разделе v2, и то шанс получить ответ, думаю, будет небольшим.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-06-22T13:21:01Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153635#p153635</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[AHK: именные мьютексы, лок и анлок]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153634#p153634" />
			<content type="html"><![CDATA[<p>Читал много статей про мьютексы, вникал в суть работы и вроде бы написал рабочие функции, но спустя время выяснилось, что они не работают.</p><p>Задача: синхронизировать потоки через именной мьютекс. Второй поток не должен начинать действовать, пока не получит мьютекс от первого потока.<br />Проблема: при каждом вызове функции lock() поток получает новый мьютекс, даже если мьютекс с таким же именем уже существует. Возможно, там каким-то образом включается рекурсия. Использую ahk v2 (h)</p><p>Код:<br /></p><div class="codebox"><pre><code>lock(name)
{
   mutex := DllCall(&quot;OpenMutex&quot;, &quot;UInt&quot;, 0x00100000, &quot;Int&quot;, null, &quot;Str&quot;, name)
   if ( mutex &gt; 0 )
   {
      result := GetAccess(mutex)
      return mutex
   }
   else
   {
      mutex := DllCall(&quot;CreateMutex&quot;, &quot;UInt&quot;, 0, &quot;UInt&quot;, 0, &quot;Str&quot;, name)
      result := GetAccess(mutex)
      return mutex
   }

}

GetAccess(mutex, timeoutms:=0xbb8) 
{
   loop 10
   {
      result := DllCall(&quot;WaitForSingleObject&quot;, &quot;UInt&quot;, mutex, &quot;UInt&quot;, timeoutms)
      if ( result != 0)
      {
         return result
      }
   }
   return &quot;ERROR&quot;
   
}

unlock(mutex) 
{
   return DllCall(&quot;ReleaseMutex&quot;, &quot;UInt&quot;, mutex)
}</code></pre></div><p>Как это выглядит в рабочем коде:</p><div class="codebox"><pre><code>
mutex := lock(&#039;some_name&#039;)
; some code...
unlock(mutex)
</code></pre></div><p>Как сделать функции рабочими?</p>]]></content>
			<author>
				<name><![CDATA[Lencore228]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=40862</uri>
			</author>
			<updated>2022-06-22T11:31:41Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153634#p153634</id>
		</entry>
</feed>
