英文:
Is a Java Lock a PThread_Mutex in C?
问题
我在C和Java中编写了完全相同的程序,其中两个线程递增一个全局计数器。为了确保对C中计数器的访问的排他性,使用了pthread_mutex_t
,而在Java中则使用synchronized(lock)
:
C版本:
#include <stdio.h>
#include <pthread.h>
static volatile int global;
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static void *run(void *arg) {
int nLoop = *((int *) arg);
int localValue;
for (int j = 0; j < nLoop; j++) {
pthread_mutex_lock(&mtx);
localValue = global;
localValue = localValue + 1;
global = localValue;
pthread_mutex_unlock(&mtx);
}
return NULL;
}
int main() {
pthread_t t1, t2;
int nLoop = 100000;
int i = 0;
while (i < 10) {
global = 0;
pthread_create(&t1, NULL, run, &nLoop);
pthread_create(&t2, NULL, run, &nLoop);
pthread_join(t1, NULL); pthread_join(t2, NULL);
printf("global: %d\n", global);
i++;
}
return 0;
}
Java版本:
public class ThreadIncr {
static int global;
private final static Object lock = new Object();
static class R implements Runnable {
int nLoop;
int localValue;
public R(int nLoop) { this.nLoop = nLoop; }
@Override
public void run() {
for(int j = 0; j < this.nLoop; j++) {
synchronized(lock) {
localValue = global;
localValue = localValue + 1;
global = localValue;
}
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1, t2;
int nLoop = 100000;
int i = 0;
while (i < 10) {
global = 0;
t1 = new Thread(new R(nLoop));
t2 = new Thread(new R(nLoop));
t1.start(); t2.start();
t1.join(); t2.join();
System.out.println("global: " + global);
i++;
}
}
}
所以基本上,Java中的锁就是C中的互斥锁(mutex)吗?
英文:
I wrote the exact same program in C and in Java, where two threads increment a global counter. To ensure exclusivity of the access on the counter in C a pthread_mutex_t
is used, while in Java synchronized(lock)
:
The C version:
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:
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?
答案1
得分: 0
在Java中,"synchronized(lock)" 表示使用所谓的 监视器锁。该监视器锁提供互斥性(链接)。因此,它提供了类似于C语言中互斥体的功能。
英文:
In Java "synchronized(lock)" means that so called monitor lock is used. That monitor lock provides mutual exclusion (link). So, it provides the same functionality as mutex in C.
专注分享java语言的经验与见解,让所有开发者获益!
评论