互斥量
/*************************************************************************\
* Copyright (C) Michael Kerrisk, 2017. *
* *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation, either version 3 or (at your option) any *
* later version. This program is distributed without any warranty. See *
* the file COPYING.gpl-v3 for details. *
\*************************************************************************/
/* Listing 30-1 */
#include <pthread.h>
#include "tlpi_hdr.h"
static volatile int glob = 0; /* "volatile" prevents compiler optimizations
of arithmetic operations on 'glob' */
static void * /* Loop 'arg' times incrementing 'glob' */
threadFunc(void *arg)
{
int loops = *((int *) arg);
int loc, j;
for (j = 0; j < loops; j++) {
loc = glob;
loc++;
glob = loc;
}
return NULL;
}
int
main(int argc, char *argv[])
{
pthread_t t1, t2;
int loops, s;
loops = (argc > 1) ? getInt(argv[1], GN_GT_0, "num-loops") : 10000000;
s = pthread_create(&t1, NULL, threadFunc, &loops);
if (s != 0)
errExitEN(s, "pthread_create");
s = pthread_create(&t2, NULL, threadFunc, &loops);
if (s != 0)
errExitEN(s, "pthread_create");
s = pthread_join(t1, NULL);
if (s != 0)
errExitEN(s, "pthread_join");
s = pthread_join(t2, NULL);
if (s != 0)
errExitEN(s, "pthread_join");
printf("glob = %d\n", glob);
exit(EXIT_SUCCESS);
}
上述代码中由于loc是分配于线程栈中的自动变量,所以每个线程都有一份,将threadFunc()内for循环中的三条语句改为glob++看似可以解决这一问题,但是在很多硬件架构上编译器依然会将这条语句转换成机器码,其执行步骤依旧等同于threadFunc循环内的三条语句。
加锁解锁互斥量
/*************************************************************************\
* Copyright (C) Michael Kerrisk, 2017. *
* *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation, either version 3 or (at your option) any *
* later version. This program is distributed without any warranty. See *
* the file COPYING.gpl-v3 for details. *
\*************************************************************************/
/* Listing 30-2 */
#include <pthread.h>
#include "tlpi_hdr.h"
static volatile int glob = 0;
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static void * /* Loop 'arg' times incrementing 'glob' */
threadFunc(void *arg)
{
int loops = *((int *) arg);
int loc, j, s;
for (j = 0; j < loops; j++) {
s = pthread_mutex_lock(&mtx);
if (s != 0)
errExitEN(s, "pthread_mutex_lock");
loc = glob;
loc++;
glob = loc;
s = pthread_mutex_unlock(&mtx);
if (s != 0)
errExitEN(s, "pthread_mutex_unlock");
}
return NULL;
}
int
main(int argc, char *argv[])
{
pthread_t t1, t2;
int loops, s;
loops = (argc > 1) ? getInt(argv[1], GN_GT_0, "num-loops") : 10000000;
s = pthread_create(&t1, NULL, threadFunc, &loops);
if (s != 0)
errExitEN(s, "pthread_create");
s = pthread_create(&t2, NULL, threadFunc, &loops);
if (s != 0)
errExitEN(s, "pthread_create");
s = pthread_join(t1, NULL);
if (s != 0)
errExitEN(s, "pthread_join");
s = pthread_join(t2, NULL);
if (s != 0)
errExitEN(s, "pthread_join");
printf("glob = %d\n", glob);
exit(EXIT_SUCCESS);
}
要锁定互斥量,在调用pthread_mutex_lock()时需要指定互斥量。如果互斥量当前处于未锁定状态,该调用将锁定互斥量并返回。如果其它线程已经锁定了这一互斥量,那么pthread_mutex_lock调用会一直阻塞,直至该互斥量被解锁。
互斥量的死锁
一个线程需要同时访问两个或更多不同的共享资源,而每个资源又由不同的互斥量管理。当超过一个线程加锁同一组互斥量时就有可能发生死锁。
假设线程A,线程B,线程A已对mutex1加锁,需要继续对mutex2加锁,线程B已对mutex2加锁,需要继续对mutex1加锁,线程AB将无限期地等待下去。
避免此类问题的一个常用方法是定义互斥量的层级关系。当多个线程对一组互斥量操作时,总是应该以相同顺序对该互斥量进行锁定。
条件变量
互斥量防止多个线程同时访问同一共享变量。条件变量允许一个线程就某个共享变量的状态通知其他线程,并让其它线程等待这一通知。
条件变量总是结合互斥量使用。条件变量就共享变量的状态改变发出通知,而互斥量则提供对该共享变量访问的互斥。
#include <time.h>
#include <pthread.h>
#include "tlpi_hdr.h"
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static int avail = 0;
static void *
threadFunc(void *arg)
{
int cnt = atoi((char *) arg);
int s, j;
for (j = 0; j < cnt; j++) {
sleep(1);
/* Code to produce a unit omitted */
s = pthread_mutex_lock(&mtx);
if (s != 0)
errExitEN(s, "pthread_mutex_lock");
avail++; /* Let consumer know another unit is available */
s = pthread_mutex_unlock(&mtx);
if (s != 0)
errExitEN(s, "pthread_mutex_unlock");
s = pthread_cond_signal(&cond); /* Wake sleeping consumer */
if (s != 0)
errExitEN(s, "pthread_cond_signal");
}
return NULL;
}
int
main(int argc, char *argv[])
{
pthread_t tid;
int s, j;
int totRequired; /* Total number of units that all threads
will produce */
int numConsumed; /* Total units so far consumed */
Boolean done;
time_t t;
t = time(NULL);
/* Create all threads */
totRequired = 0;
for (j = 1; j < argc; j++) {
totRequired += atoi(argv[j]);
s = pthread_create(&tid, NULL, threadFunc, argv[j]);
if (s != 0)
errExitEN(s, "pthread_create");
}
/* Loop to consume available units */
numConsumed = 0;
done = FALSE;
for (;;) {
s = pthread_mutex_lock(&mtx);
if (s != 0)
errExitEN(s, "pthread_mutex_lock");
while (avail == 0) { /* Wait for something to consume */
s = pthread_cond_wait(&cond, &mtx);
if (s != 0)
errExitEN(s, "pthread_cond_wait");
}
/* At this point, 'mtx' is locked... */
while (avail > 0) { /* Consume all available units */
/* Do something with produced unit */
numConsumed ++;
avail--;
printf("T=%ld: numConsumed=%d\n", (long) (time(NULL) - t),
numConsumed);
done = numConsumed >= totRequired;
}
s = pthread_mutex_unlock(&mtx);
if (s != 0)
errExitEN(s, "pthread_mutex_unlock");
if (done)
break;
/* Perhaps do other work here that does not require mutex lock */
}
exit(EXIT_SUCCESS);
}
与pthread_cond_wait函数相关的代码如下:
s = pthread_mutex_lock(&mtx);
if (s != 0)
errExitEN(s, "pthread_mutex_lock");
while (avail == 0) { /* Wait for something to consume */
s = pthread_cond_wait(&cond, &mtx);
if (s != 0)
errExitEN(s, "pthread_cond_wait");
}
pthread_cond_wait()函数在执行时会首先解锁互斥量mtx,然后阻塞调用线程,直到另一个线程就条件变量cond发出信号,然后再去重新锁定mtx.
上述代码中必须使用while而不能使用if的原因是:当代码从pthread_cond_wait()返回时,并不判定条件的状态,所以应该立即重新检查判断条件,如果条件不满足则继续休眠等待。
原因如下: