- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个运行 200 个线程并在每个线程上递增 1000 次的程序,因此最终结果必须是 200000这在使用互斥体时
我编译代码时没有警告,但是当我运行程序时,它给了我这个错误:错误 - pthread_mutex_lock() 失败:参数无效
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <pthread.h>
#include "debug.h"
#include "memory.h"
#define NUM_THREADS 200
#define C_ERRO_PTHREAD_CREATE 1
#define C_ERRO_PTHREAD_JOIN 2
#define C_ERRO_MUTEX_INIT 3
#define C_ERRO_MUTEX_DESTROY 4
#define C_ERRO_MUTEX 5
void *task(void *arg);
typedef struct
{
int contador;
pthread_mutex_t mutex;
}thread_params_t;
int main(int argc, char *argv[])
{
pthread_t tids[NUM_THREADS];
thread_params_t thread_params;
/* Disable warnings */
(void)argc; (void)argv;
/* Mutex */
if ((errno = pthread_mutex_init(&thread_params.mutex, NULL)) != 0)
ERROR(C_ERRO_MUTEX_INIT, "pthread_mutex_init() failed!");
/* Contador */
thread_params.contador = 0;
// Criação das threads + passagem de parametro
for (int i = 0; i < NUM_THREADS; i++){
if ((errno = pthread_create(&tids[i], NULL, task, &thread_params)) != 0)
ERROR(10, "Erro no pthread_create()!");
}
// Espera que todas as threads terminem
for (int i = 0; i < NUM_THREADS; i++){
if ((errno = pthread_join(tids[i], NULL)) != 0)
ERROR(11, "Erro no pthread_join()!\n");
}
/* Mostra valor do campo contador */
printf("Contador = %d\n", thread_params.contador);
/* Destroi o mutex */
if ((errno = pthread_mutex_destroy(&thread_params.mutex)) != 0)
ERROR(C_ERRO_MUTEX_DESTROY, "pthread_mutex_destroy() failed!");
return 0;
}
// Thread
void *task(void *arg)
{
pthread_mutex_t mutex;
// cast para o tipo de dados enviado pela 'main thread'
thread_params_t *params = (thread_params_t *) arg;
usleep(100*(random() % 2)); /* Adormece entre 0 a 100 usecs */
//inicio secção critica
if ( (errno = pthread_mutex_lock(&mutex)) != 0)
ERROR(C_ERRO_MUTEX, "pthread_mutex_lock() failed");
int cont = params->contador;
sched_yield();
usleep(100*(random() % 2)); /* Adormece entre 0 a 100 usecs */
params->contador = cont + 1000 ;
//fim secção critica
if ( (errno = pthread_mutex_unlock(&mutex)) != 0)
ERROR(C_ERRO_MUTEX, "pthread_mutex_unlock() failed");
return NULL;
}
最佳答案
已经找到了答案,在线程中我需要创建一个指针,params->mutex,因为我的结构,因此我不需要再次声明互斥体
void *task(void *arg)
{
// cast para o tipo de dados enviado pela 'main thread'
thread_params_t *params = (thread_params_t *) arg;
//inicio secção critica
if ( (errno = pthread_mutex_lock(¶ms->mutex)) != 0)
ERROR(C_ERRO_MUTEX, "pthread_mutex_lock() failed");
int cont = params->contador;
sched_yield();
params->contador = cont + 1000 ;
//fim secção critica
if ( (errno = pthread_mutex_unlock(¶ms->mutex)) != 0)
ERROR(C_ERRO_MUTEX, "pthread_mutex_unlock() failed");
return NULL;
}
关于c - 错误 - pthread_mutex_lock() 失败 : Invalid argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46808952/
我在一个线程中调用了一个pthread_mutex_lock(&th) 然后我想在另一个线程中解锁互斥量pthread_mutex_unlock(&th) 这有可能吗? 或者应该在同一个线程中解锁互斥
我真的很想知道为什么所有的源代码都实现了 pthread_mutex_lock 从不测试其定义的返回值: documentation of pthread 即使在书中,示例也不会测试锁定是否出错,代码
故事 根据手册页https://linux.die.net/man/3/pthread_mutex_lock The mutex object referenced by mutex shall be
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 编辑问题以包含 desired behavior, a specific problem
相关代码可以在这里找到:http://pastebin.com/VbhtQckm 问题出在线上85。 pthread_mutex_lock(ID_retrieval_pool->info->lock)
在我的生产者/消费者练习中,我想构建两个线程,一个用于生产者,另一个用于消费者。他们可以访问缓冲区以在其上读取/写入消息,并且我想使用互斥体来管理此缓冲区。 #include #include #
我想编写一个程序,计数为100。我想使用pthread锁使用10个线程来完成此任务。当程序进入线程时,它会生成一个介于0到2之间的数字,该值将被添加到其数组的索引以及全局变量sum中。当总和达到100
如何创建一个锁来锁定不同线程进入该函数?线程 1:A线程 2:C主题 3:C线程 4:B线程 5:C主题 6:B “A”、“B”、“C”是创建线程时传递给线程的参数。 所以当我锁定这一个锁X时,所有传
我有一个可以打印一些数据结构的代码。 void print_wheel_timer(wheel_timer_t *wt){ } Then i sandwiched the code in-betw
我正在尝试模拟一个多线程环境,每个线程都调用 Kevin 类的 Speak 函数。但是,在 pthread_mutex_lock 函数(之后/期间)存在段错误,我找不到原因。 pthread_mute
一个普遍的事实是,pthread_mutex_lock( ) 应该始终应用于线程函数中使用的全局变量和静态变量。线程函数中的局部变量怎么样? 在哪些用例中,线程函数内的局部变量应该被锁定或应该始终被锁
最近看了一些关于thread mutex的代码,相关代码在这里: #include #include #include pthread_mutex_t mutex; pthr
当我尝试这样做时出现段错误 pthread_mutex_lock(&_mutex). 这真的很奇怪,我不确定是什么原因造成的。我已经在构造函数中初始化了 _mutex pthread_mutex_in
我正在使用 pthread_mutex_t 进行锁定。 pthread_mutex_t m_lock; void get1() { cout<<"Start get 1"<
我正在编写一个程序来读取和处理大量数据。为了加快这个过程,我使用 C++ 的 Pthreads 库实现了多线程读取/处理。但是,当我在我的互斥量上调用 pthread_mutex_lock(&lock
我正在使用上面的代码使用 2 个线程递增计数器,这两个线程独立地获取 mut 锁和递增计数器。线程进入此函数后,我面临死锁。 pthread_mutex_t mut = PTHREAD_MUTEX_
这个问题在这里已经有了答案: Does guarding a variable with a pthread mutex guarantee it's also not cached? (3 个答案
我正在编写一些 C 代码,但在锁定互斥体时遇到了问题。该代码调用了一个函数,并且该函数锁定了一个互斥锁以确保文件指针不会被覆盖,这在多个实例中都可以正常工作,可能对被调用函数进行了大约 10-20 次
#include "MutexCondition.h" bool MutexCondition::init(){ printf("MutexCondition::init called\n")
我只是想知道与线程之间的同步相关的功能是如何在 Unix 中实现的。例如,当我调用 pthread_mutex_lock 时会发生什么?有没有使用中的指针?对源代码的引用会很有帮助。 最佳答案 它既复
我是一名优秀的程序员,十分优秀!