python中由于全局锁(GIL)的存在导致多线程并不能利用多核,看了一些资料说,java好像对多线程的处理是可以利用多核的硬件资源的(因为java直接调用的操作系统的多线程接口处理的)。不同的语言对多线程的处理是否能做到利用硬件,主要取决于编译器或者解释器对线程的包装吗?还有python中多进程是怎样一个概念,不是说一个程序只有一个进程,进程里面可以有多个线程,那么python是怎么在一个程序中用多个进程的?
Python中由于全局锁(GIL)的存在导致多线程并不能利用多核,看了一些资料说,Java好像对多线程的处理是可以利用多核的硬件资源的(因为 Java直接调用的操作系统的多线程接口处理的)。不同的语言对多线程的处理是否能做到利用硬件,主要取决于编译器或者解释器对线程的包装吗?
<code class="language-c"><span class="kt">void</span> <span class="nf">PyEval_AcquireLock</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyThread_acquire_lock</span><span class="p">(</span><span class="n">interpreter_lock</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="kt">void</span> <span class="nf">PyEval_ReleaseLock</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyThread_release_lock</span><span class="p">(</span><span class="n">interpreter_lock</span><span class="p">);</span> <span class="p">}</span> <span class="kt">void</span> <span class="nf">PyEval_AcquireThread</span><span class="p">(</span><span class="n">PyThreadState</span> <span class="o">*</span><span class="n">tstate</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">tstate</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="n">Py_FatalError</span><span class="p">(</span><span class="s">"PyEval_AcquireThread: NULL new thread state"</span><span class="p">);</span> <span class="cm">/* Check someone has called PyEval_InitThreads() to create the lock */</span> <span class="n">assert</span><span class="p">(</span><span class="n">interpreter_lock</span><span class="p">);</span> <span class="n">PyThread_acquire_lock</span><span class="p">(</span><span class="n">interpreter_lock</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyThreadState_Swap</span><span class="p">(</span><span class="n">tstate</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="n">Py_FatalError</span><span class="p">(</span> <span class="s">"PyEval_AcquireThread: non-NULL old thread state"</span><span class="p">);</span> <span class="p">}</span> <span class="kt">void</span> <span class="nf">PyEval_ReleaseThread</span><span class="p">(</span><span class="n">PyThreadState</span> <span class="o">*</span><span class="n">tstate</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">tstate</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="n">Py_FatalError</span><span class="p">(</span><span class="s">"PyEval_ReleaseThread: NULL thread state"</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyThreadState_Swap</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span> <span class="o">!=</span> <span class="n">tstate</span><span class="p">)</span> <span class="n">Py_FatalError</span><span class="p">(</span><span class="s">"PyEval_ReleaseThread: wrong thread state"</span><span class="p">);</span> <span class="n">PyThread_release_lock</span><span class="p">(</span><span class="n">interpreter_lock</span><span class="p">);</span> <span class="p">}</span> </code>