java mutex命令_互斥量mutex的简单使用(实例讲解)

几个重要的函数:

#include

int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutex_t *restrict attr); //初始化mutex

int pthread_mutex_destroy(pthread_mutex_t *mutex); //如果mutex是动态分配的,则释放内存前调用此函数。

int pthread_mutex_lock(pthread_mutex_t *mutex); //加锁

int pthread_mutex_trylock(pthread_mutex_t *mutex); //若已有其他线程占用锁,则返回EBUSY,否则返回0,不阻塞。

int pthread_mutex_unlock(pthread_mutex_t *mutex); //解锁

例程:

复制代码 代码如下:

#include

#include

#include

#include

#include

int a = 100;

int b = 200;

pthread_mutex_t lock;

void * threadA()

{

pthread_mutex_lock(&lock);

printf("thread A got lock! ");

a -= 50;

sleep(3); //如果不加锁,threadB输出会是50和200

b += 50; //加锁后会sleep 3秒后,并为b加上50 threadB才能打印

pthread_mutex_unlock(&lock);

printf("thread A released the lock! ");

a -= 50;

}

void * threadC()

{

sleep(1);

while(pthread_mutex_trylock(&lock) == EBUSY) //轮询直到获得锁

{

printf("thread C is trying to get lock! ");

usleep(100000);

}

printf("thread C got the lock! ");

a = 1000;

b = 2000;

pthread_mutex_unlock(&lock);

printf("thread C released the lock! ");

}

void * threadB()

{

sleep(2); //让threadA能先执行

pthread_mutex_lock(&lock);

printf("thread B got the lock! a=%d b=%d ", a, b);

pthread_mutex_unlock(&lock);

printf("thread B released the lock! ", a, b);

}

int main()

{

pthread_t tida, tidb, tidc;

pthread_mutex_init(&lock, NULL);

pthread_create(&tida, NULL, threadA, NULL);

pthread_create(&tidb, NULL, threadB, NULL);

pthread_create(&tidc, NULL, threadC, NULL);

pthread_join(tida, NULL);

pthread_join(tidb, NULL);

pthread_join(tidc, NULL);

return 0;

}

时间: 2014-01-20

几个重要的函数: #include int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutex_t *restrict attr); //初始化mutex int pthread_mutex_destroy(pthread_mutex_t *mutex); //如果mutex是动态分配的,则释放内存前调用此函数。 int pthread_mutex_lock(pthread_mutex_t *mutex); //加锁 int pthread_mutex_trylock(pthread_mutex_t *mutex); //若已有其他线程占用锁,则返回EBUSY,否则返回0,不阻塞。 int pthread_mutex_unlock(pthread_mutex_t *mutex); //解锁 例程: 复制代码 代码如下: #include #include #include #include #include int a = 100; int b = 200; pthread_mutex_t lock; void * threadA() { pthread_mutex_lock(&lock); printf("thread A got lock! "); a -= 50; sleep(3); //如果不加锁,threadB输出会是50和200 b += 50; //加锁后会sleep 3秒后,并为b加上50 threadB才能打印 pthread_mutex_unlock(&lock); printf("thread A released the lock! "); a -= 50; } void * threadC() { sleep(1); while(pthread_mutex_trylock(&lock) == EBUSY) //轮询直到获得锁 { printf("thread C is trying to get lock! "); usleep(100000); } printf("thread C got the lock! "); a = 1000; b = 2000; pthread_mutex_unlock(&lock); printf("thread C released the lock! "); } void * threadB() { sleep(2); //让threadA能先执行 pthread_mutex_lock(&lock); printf("thread B got the lock! a=%d b=%d ", a, b); pthread_mutex_unlock(&lock); printf("thread B released the lock! ", a, b); } int main() { pthread_t tida, tidb, tidc; pthread_mutex_init(&lock, NULL); pthread_create(&tida, NULL, threadA, NULL); pthread_create(&tidb, NULL, threadB, NULL); pthread_create(&tidc, NULL, threadC, NULL); pthread_join(tida, NULL); pthread_join(tidb, NULL); pthread_join(tidc, NULL); return 0; } 时间: 2014-01-20
经验分享 程序员 微信小程序 职场和发展