English 中文(简体)
为什么使用read-mut-锁,如果能够以可规划的方式这样做?
原标题:Why pthread_mutex_lock is used, when the same can be done in a programmable way?

我们都知道烟雾和严重的问题。

在校对中,可以采用pthread_mutex_lock( )pthread_mutex_unlock()。

But why do we need these system calls, when the same can be implemented in the code, by doing something like:

flag = 0;
if (flag) // Thread1 enters and makes flag = 0
{
   flag = 0; // On entering critical section, flag is made 0 so that others can t enter
  // do some critical section operation
  flag = 1;
}
// Thread1 exits

Doing the same as above, will it solve the critical section problem? If no, then why?

最佳回答

There are probably many reasons why pthread_mutex objects and APIs that manipulate those objects are used instead of everyone coding up their own synchronization primitives. A coupl eof the more important ones:

  • much like many other objects and functionality that are useful to a wide audience, it makes sense to standardize that functionality so that people don t have to reinvent the wheel and so they can use and recognize standard patterns and idioms. In other words, there are pthread mutex APIs for the same reason there are standard string manipulation functions.

  • synchronization techniques are notoriously complicated and difficult to get right. So it s best to have a vetted library of code that performs this functionality. Even if it were OK to reinvent the wheel umpteen million times, having 99% of those implementations with serious flaws isn t a great situation. For example, pthreads handles issues like memory barriers and atomicity which are not addressed properly in the example you have in your question. Considering the example in the question: there s at least one serious problem; it has a race condition where two threads could enter the critical section concurrently since the test of the flag and setting it to 0 aren t performed atomically.

问题回答

First, if your code would work, the second thread will skip entirely the critical section. You d have to place a loop or something there.

此外,考虑到时间表可能预先阻止你在任何地方的read子。 如果A做过测试并在修改<代码>flag <>/code>之前先发制人,而B版允许测试,随后很快进入关键部分。 你们有两条read子。

首先,你应利用原子记忆作业(见国际乐施会间交流和海合会_val_compare_and_swap()。

Secondly, this code will work, but only if second thread shouldn t wait when first sets flag back to 1. If it should, you wil end with loop, which would consume all your CPU. In that case you should use something, that will cause waiting thread to sleep (for example pthread_mutex_lock()).

由于你把问题与“次要”挂钩,人们可能会补充说,在所谓的“futexes”或“fastuserspace mutexes”之上建有read。 由于名称可能意味着,快行道,即锁定和锁定一个未中断的航道,NOT确实需要进行扫描,它都在使用空间进行。 FWIW, AFAIK Windows也相似。





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签