简体   繁体   English

Java 是否锁定了 C 中的 PThread_Mutex?

[英]Is a Java Lock a PThread_Mutex in C?

I wrote the exact same program in C and in Java, where two threads increment a global counter.我在 C 和 Java 中编写了完全相同的程序,其中两个线程递增一个全局计数器。 To ensure exclusivity of the access on the counter in C a pthread_mutex_t is used, while in Java synchronized(lock) :为了确保在 C 中的计数器上访问的排他性,使用了pthread_mutex_t ,而在 Java 中使用了synchronized(lock)

The C version: C 版本:

  1 #include <stdio.h>
  2 #include <pthread.h>
  3 
  4 static volatile int global;
  5 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
  6 
  7 static void *run(void *arg) {
  8     int nLoop = *((int *) arg);
  9     int localValue;
 10 
 11 
 12 
 13 
 14 
 15     for (int j = 0; j < nLoop; j++) {
 16         pthread_mutex_lock(&mtx);
 17         localValue = global;
 18         localValue = localValue + 1;
 19         global     = localValue;
 20         pthread_mutex_unlock(&mtx);
 21     }
 22     return NULL;
 23 }
 24 
 25 int main() {
 26     pthread_t t1, t2;
 27     int nLoop = 100000;
 28 
 29     int i = 0;
 30     while (i<10) {
 31         global = 0;
 32         pthread_create(&t1, NULL, run, &nLoop);
 33         pthread_create(&t2, NULL, run, &nLoop);
 34 
 35         pthread_join(t1, NULL); pthread_join(t2, NULL);
 36         printf("global: %d\n", global);
 37         i++;
 38     }
 39     return 0;
 40 }

The Java version: Java版本:

  1 public class ThreadIncr {
  2 
  3 
  4     static int global;
  5     private final static Object lock = new Object();
  6 
  7     static class R implements Runnable {
  8         int nLoop;
  9         int localValue;
 10 
 11         public R(int nLoop) { this.nLoop = nLoop; }
 12 
 13         @Override
 14         public void run() {
 15             for(int j = 0; j<this.nLoop; j++) {
 16                 synchronized(lock) {
 17                     localValue = global;
 18                     localValue = localValue + 1;
 19                     global = localValue;
 20                 }
 21             }
 22         }
 23     }
 24 
 25     public static void main(String[] args) throws InterruptedException {
 26         Thread t1, t2;
 27         int nLoop = 100000;
 28 
 29         int i = 0;
 30         while (i < 10) {
 31             global = 0;
 32             t1 = new Thread(new R(nLoop));
 33             t2 = new Thread(new R(nLoop));
 34             t1.start(); t2.start();
 35             t1.join();  t2.join();
 36             System.out.println("global: " + global);
 37             i++;
 38         }
 39     }
 40 }

So basically the lock of Java is the mutex in C?那么基本上Java的锁就是C中的互斥锁了?

In Java "synchronized(lock)" means that so called monitor lock is used.在 Java 中,“synchronized(lock)”表示使用了所谓的监视器锁 That monitor lock provides mutual exclusion ( link ).该监视器锁提供互斥( 链接)。 So, it provides the same functionality as mutex in C.因此,它提供了与 C 中的互斥锁相同的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM