在结构中使用指向 Mutex (pthread_mutex_t *) 的指针,而不是全局 Mutex
Using a pointer to a Mutex (pthread_mutex_t *) in a structure, Instead of a global Mutex
我在启动多个线程的程序中使用锁 (pthread_mutex_t),所有线程都收到一个指向结构的指针,该结构包含指向此互斥体的指针。
所以有一个初始化的互斥量,所有结构都有一个指向它的指针,但效果不佳,我不明白为什么?
这里是一些示例代码,我尽量把它写得最少。
让我们调用我的程序 duck
:
duck.c
# include "duck.h"
void *exec_threads(void *arg)
{
struct s_duck *duck;
duck = (struct s_duck*)arg;
pthread_mutex_lock(duck->mutex);
printf("duck nbr %i\n", duck->n);
pthread_mutex_unlock(duck->mutex);
return (NULL);
}
int main(void)
{
pthread_t *id;
pthread_mutex_t *mutex;
struct s_duck *duck;
int n;
int i;
n = 5; // number of threads
id = malloc(sizeof(int) * n); // allocate id[n]
mutex = malloc(sizeof(pthread_mutex_t)); // allocate mutex
pthread_mutex_init(mutex, NULL); // init mutex
duck = init_chain_ducks(mutex, n); // create chained list of structure
i = 0;
while (i < n)
{
pthread_create(&id[i], NULL, &exec_threads, duck); // launch threads
duck = duck->next;
i++;
}
i = 0;
while (i < n)
{
pthread_join(id[i], NULL); // join threads
i++;
}
write(1, "the end\n", 8);
return (0);
}
duck_chain.c
# include "duck.h"
struct s_duck *init_chain_ducks(pthread_mutex_t *mutex, int n)
{
struct s_duck *new;
struct s_duck *duck;
int i;
i = n;
duck = NULL;
while (i > 0)
{
new = malloc(sizeof(struct s_duck));
new->n = i;
new->mutex = mutex; // this is where the pointer to the mutex is stored
new->next = duck;
duck = new;
i--;
}
return (duck);
}
duck.h
#ifndef DUCK_H
# define DUCK_H
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <pthread.h>
struct s_duck
{
int n;
pthread_mutex_t *mutex;
struct s_duck *next;
};
struct s_duck *init_chain_ducks(pthread_mutex_t *mutex, int n);
#endif
如果我为互斥锁使用全局变量,效果会很好。但是对于结构中的指针,我得到了类似未定义行为的东西:
大多数时候我得到这个输出:
duck nbr 1
duck nbr 3
duck nbr 4
duck nbr 5
duck nbr 2
[2] 18914 segmentation fault (core dumped) ./a.out
段错误发生在 pthread_join()
期间,但我也经常遇到这两个错误:
duck nbr 1
duck nbr 2
[wait indefinitely]
或者:
duck nbr 1
duck nbr 4
duck nbr 3
a.out: ../nptl/pthread_mutex_lock.c:81: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed.
[2] 20394 abort (core dumped) ./a.out
我一定是犯了一些基本错误,但我不知道在哪里。
(编译:gcc duck.c duck_chain.c -lpthread
)
pthread_t *id;
id = malloc(sizeof(int) * n); // allocate id[n]
id
不是 int
- 它是 pthread_t
。应该是sizeof(pthread_t)
。使用一个小技巧,使用指针指向。
id = malloc(sizeof(*id) * n);
使用 -g -Wall -Wextra -fsanitize=undefined,address
编译 - sanitizer 将帮助您发现此类错误。
我在启动多个线程的程序中使用锁 (pthread_mutex_t),所有线程都收到一个指向结构的指针,该结构包含指向此互斥体的指针。
所以有一个初始化的互斥量,所有结构都有一个指向它的指针,但效果不佳,我不明白为什么?
这里是一些示例代码,我尽量把它写得最少。
让我们调用我的程序 duck
:
duck.c
# include "duck.h"
void *exec_threads(void *arg)
{
struct s_duck *duck;
duck = (struct s_duck*)arg;
pthread_mutex_lock(duck->mutex);
printf("duck nbr %i\n", duck->n);
pthread_mutex_unlock(duck->mutex);
return (NULL);
}
int main(void)
{
pthread_t *id;
pthread_mutex_t *mutex;
struct s_duck *duck;
int n;
int i;
n = 5; // number of threads
id = malloc(sizeof(int) * n); // allocate id[n]
mutex = malloc(sizeof(pthread_mutex_t)); // allocate mutex
pthread_mutex_init(mutex, NULL); // init mutex
duck = init_chain_ducks(mutex, n); // create chained list of structure
i = 0;
while (i < n)
{
pthread_create(&id[i], NULL, &exec_threads, duck); // launch threads
duck = duck->next;
i++;
}
i = 0;
while (i < n)
{
pthread_join(id[i], NULL); // join threads
i++;
}
write(1, "the end\n", 8);
return (0);
}
duck_chain.c
# include "duck.h"
struct s_duck *init_chain_ducks(pthread_mutex_t *mutex, int n)
{
struct s_duck *new;
struct s_duck *duck;
int i;
i = n;
duck = NULL;
while (i > 0)
{
new = malloc(sizeof(struct s_duck));
new->n = i;
new->mutex = mutex; // this is where the pointer to the mutex is stored
new->next = duck;
duck = new;
i--;
}
return (duck);
}
duck.h
#ifndef DUCK_H
# define DUCK_H
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <pthread.h>
struct s_duck
{
int n;
pthread_mutex_t *mutex;
struct s_duck *next;
};
struct s_duck *init_chain_ducks(pthread_mutex_t *mutex, int n);
#endif
如果我为互斥锁使用全局变量,效果会很好。但是对于结构中的指针,我得到了类似未定义行为的东西:
大多数时候我得到这个输出:
duck nbr 1
duck nbr 3
duck nbr 4
duck nbr 5
duck nbr 2
[2] 18914 segmentation fault (core dumped) ./a.out
段错误发生在 pthread_join()
期间,但我也经常遇到这两个错误:
duck nbr 1
duck nbr 2
[wait indefinitely]
或者:
duck nbr 1
duck nbr 4
duck nbr 3
a.out: ../nptl/pthread_mutex_lock.c:81: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed.
[2] 20394 abort (core dumped) ./a.out
我一定是犯了一些基本错误,但我不知道在哪里。
(编译:gcc duck.c duck_chain.c -lpthread
)
pthread_t *id;
id = malloc(sizeof(int) * n); // allocate id[n]
id
不是 int
- 它是 pthread_t
。应该是sizeof(pthread_t)
。使用一个小技巧,使用指针指向。
id = malloc(sizeof(*id) * n);
使用 -g -Wall -Wextra -fsanitize=undefined,address
编译 - sanitizer 将帮助您发现此类错误。