- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好,对于我遇到的一个小问题,如果能提供任何帮助,我们将不胜感激。基本上我的程序运行良好,但没有通过所有必需的测试。显然问题不在于执行以下操作之一:
谁能看出我哪里出错了?非常感谢任何帮助,谢谢
#include "counter.h"
/* ============================================================================
* File-global variables
* ========================================================================== */
static int ncounters = 0;
static struct counter *counters = NULL;
static int nthreads = 0;
static int *ninstructions = NULL;
static struct instruction **instructions = NULL;
/* ============================================================================
* Operations
* ========================================================================== */
static void
decrement(long long *n) {
*n = *n-1;
}
static void
increment(long long *n) {
*n = *n+1;
}
static void
mult2(long long *n) {
long long s = 2;
long long t = *n;
long long q = t*s;
*n = q;
}
/* ============================================================================
* Helper functions
* ========================================================================== */
int
quit(void) {
int i;
for (i=0; i<nthreads; ++i) {
free (instructions[i]);
}
free (instructions);
for (i=0; i<ncounters; ++i) {
pthread_mutex_t *my = &(counters[i].mylock);
pthread_mutex_destroy(my);
}
free (counters);
free (ninstructions);
return 0;
}
/* ============================================================================
* Thread function
* ========================================================================== */
static void *
worker_thread(void *arg) {
int t = *((int*)arg);
int l;
for (l = 0; l<ninstructions[t]; ++l) {
int y;
struct instruction* curr = &instructions[t][l];
pthread_mutex_lock(&curr->counter->mylock);
for (y=0; y<curr->repetitions; ++y) {
long long *g = &curr->counter->counter;
(curr->work_fn)(g);
}
pthread_mutex_unlock(&curr->counter->mylock);
}
return NULL;
}
/* ============================================================================
* Main function
* ========================================================================== */
int
main(void) {
if (scanf("%d", &ncounters) != 1 || ncounters < 1) {
printf("error\n");
return quit();
}
counters = (struct counter*)malloc(ncounters*sizeof(struct counter));
if (scanf(" %d", &nthreads) != 1 || nthreads < 1) {
printf("error\n");
return quit();
}
ninstructions = (int *)malloc(nthreads*sizeof(int));
instructions = (struct instruction**)malloc(nthreads*sizeof(struct instruction*));
int i;
for (i=0; i<nthreads; ++i) {
if (scanf(" %d", &ninstructions[i]) != 1) {
printf("error\n");
return quit();
}
instructions[i] = (struct instruction*)malloc(ninstructions[i]*sizeof(struct instruction));
int k;
for (k=0; k<ninstructions[i]; ++k) {
int c, r;
char f;
if (scanf(" %d %c %d", &c, &f, &r) != 3 || c>ncounters-1) {
printf("error\n");
return quit();
}
struct instruction* curr = &instructions[i][k];
struct counter* currcp = &counters[c];
pthread_mutex_init (&currcp->mylock, NULL);
curr->counter = currcp;
curr->repetitions = r;
switch(f) {
case 'I':
curr->work_fn = increment;
break;
case 'D':
curr->work_fn = decrement;
break;
case '2':
curr->work_fn = mult2;
break;
default:
printf("error\n");
return quit();
}
}
}
int w;
pthread_t threadIDs[nthreads];
int args[nthreads];
for (w=0; w<nthreads; ++w) {
args[w] = w;
pthread_create(&threadIDs[w], NULL, worker_thread, (void *) &args[w]);
}
int u;
for (u=0; u<nthreads; ++u) {
pthread_join(threadIDs[u], NULL);
}
int d;
for (d=0; d<ncounters; ++d) {
printf("%lld\n", counters[d].counter);
}
return quit();
}
和数据结构
#ifndef __COUNTER_H__
#define __COUNTER_H__
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
/* counter data structure */
struct counter {
pthread_mutex_t mylock;
long long counter; /* to store counter */
};
/* counter value */
struct instruction {
struct counter *counter; /* pointer to counter */
int repetitions; /* number of repetitions */
void (*work_fn)(long long *); /* function pointer to work function */
};
#endif
是的,抱歉,我认为没有格式可能是可能的:
<number of counters>
<number of threads>
<instruction-sequence>
<instruction-sequence>
....
<number of instructions>
<instruction>
<instruction>
....
因此对于每个指令序列,您都有一个指令列表,这些指令由每个线程在一个或多个计数器上执行。即输入:
2
2
1
0 I 10
2
1 D 10
1 2 2
将产生结果:
10
-40
(只有三种指令类型:自增(I),自减(D),乘以2(2)其中指令的格式为:
<counter> <function> <repitition>
这有道理吗?
最佳答案
好吧,看起来您正在加入您创建的所有线程,并解锁您锁定的所有 mutices —— 这部分相当简单。
然而,从第二个条件来看,mutices 的创建和销毁之间似乎没有明显的相关性。
每条指令调用一次 pthread_mutex_init
,而每条计数器调用一次 pthread_mutex_destroy
。我看不到指令数等于计数器数的任何保证。
我想您希望每个计数器有一个互斥量。因此,为每条指令初始化一个互斥锁是没有意义的。您可能需要一个初始化例程,它为每个计数器初始化一个互斥锁,以反射(reflect)您的 quit
例程,该例程会破坏每个计数器的互斥锁。
关于c++ - 免费互斥问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5856055/
前言 俗话说得好“工欲善其事,必先利其器”,合理的选择和使用可视化的管理工具可以降低技术入门和使用的门槛。今天大姚给大家分享一款.NET Avalonia开源、免费、跨平台、快速的Git可视化管理工
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
正在学习有关 C 语言链接列表的教程。我已编译此代码并通过 valgrind 运行它。它显示了 4 次分配和 0 次释放,这是我理解的。我需要知道如何正确调用 free() 来释放分配。 代码示例:l
正如标题所说,我需要一个搜索引擎...用于mysql 搜索。我的网站是基于 PHP 的。 我打算使用 sphinx,但我的托管公司不支持全文索引! 所以一个没有全文的搜索引擎! 它应该是相当强大的,并
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 2 年前。
我正在寻找稳定和成熟的免费/开源库来比较两个图像。 我找到了这个,但我想知道你是否使用更好的! Similar images finder - .NET Image processing in C#
我有一个通用链表实现,其中包含一个指向数据的 void* 的节点结构和一个包含对头的引用的列表结构。现在这是我的问题,链表中的一个节点可能通过其 void* 持有对另一个链表的引用。当我释放包含较小列
前言 今天大姚给大家分享一款开源(MIT License)、免费、现代化风格的WPF UI控件库:ModernWpf。 项目介绍 ModernWpf是一个开源项目,它为 WPF 提供了一组现代化
LiveCharts2 LiveCharts2是一个.NET开源(MIT License)、简单、灵活、交互式且功能强大的.NET图表、地图和仪表,现在几乎可以在任何地方运行如:Maui、Uno P
前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、实用的多功能原神工具箱,旨在改善桌面端玩家的游戏体验:胡桃工具箱。 工具箱介绍 胡桃工具箱是一款.NET开源(MIT
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 3年前关闭。 Improve this qu
当我这样做时,我的 meteor 应用程序运行的免费服务器的规范是什么。 meteor deploy myapp.meteor.com 规范方面 Storage size Max bandwidth
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
如果可能,我可以使用任何网络服务免费存储少量数据(考虑 XML 或 JSON)? 我想我想创建一个小型待办事项应用程序,只是探索/学习(最好是免费的),它还可以将数据备份到云端,以便他们可以在智能手机
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 2年前关闭。 Improve this qu
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
是否有任何免费/开源替代 Codesmith 可以在功能上进行比较并生成 .NET 代码? 最佳答案 几年前我做了同样的研究,发现 MyGeneration 成为 非常好。 关于.net - 免费 C
在尝试找到可以逐步执行/允许线程的haskell monad时,我发现了免费的monad data Free f a = Return a | Roll (f (Free f a)) 及其 monad
我是一名优秀的程序员,十分优秀!