- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个涉及线程和互斥体的 C 小型项目。我正在开发的程序对 bmp 图像应用过滤器。该项目的目标是实现一个能够处理此命令行的程序:
$ ./filter -f filter1[,filter2[,...]] -t numThreads1[,numThreads2[,...]] input-folder output-folder
其中 -f 是我要应用的过滤器(“红色”、“蓝色”、“绿色”、“灰度”和“模糊”),-t 是每个过滤器分配的线程数。
到目前为止,除了模糊之外,一切都很好,我陷入了数据竞争(或者,我认为是这样)。模糊滤镜的工作原理如下:
/* Add a Gaussian blur to an image using
* this 3X3 matrix as weights matrix:
* 0.0 0.2 0.0
* 0.2 0.2 0.2
* 0.0 0.2 0.0
*
* If we consider the red component in this image
* (every element has a value between 0 and 255)
*
* 1 2 5 2 0 3
* -------
* 3 |2 5 1| 6 0 0.0*2 + 0.2*5 + 0.0*1 +
* | |
* 4 |3 6 2| 1 4 -> 0.2*3 + 0.2*6 + 0.2*2 + -> 3.2
* | |
* 0 |4 0 3| 4 2 0.0*4 + 0.2*0 + 0.0*3
* -------
* 9 6 5 0 3 9
*
* The new value of the pixel (3, 4) is round(3.2) = 3.
*
* If a pixel is outside the image, we increment the central pixel weight by 0.2
* So the new value of pixel (0, 0) is:
* 0.2 * 0 + 0.2 * 9 + 0.2 * 6 + 0.2 * 9 + 0.2 * 9 = 6.6 -> 7
*/
问题是,当我使用这个模糊滤镜在“棋盘”图像上运行我的程序时:
$ ./filter -f blur -t 8 chess.bmp chessBlur.bmp
我期待得到这个image ,但我得到 this (“断”线随机变化)
我使用互斥锁来锁定和解锁关键部分,但正如您所看到的,数据争用仍然发生。在我的过滤器上只有两个字,我一次给每个线程一行,从底部开始向上。我的 filter_blur 代码是:
int filter_blur(struct image *img, int nThread)
{
int error = 0;
int mod = img->height%nThread;
if (mod > 0)
mod = 1;
pthread_t threads[nThread];
pthread_mutex_t mutex;
args arguments[nThread];
struct image* img2 = (struct image*)malloc(sizeof(struct image));
memcpy(img2,img,sizeof(struct image));
error=pthread_mutex_init( &mutex, NULL);
if(error!=0)
err(error,"pthread_mutex_init");
int i = 0;
for (i=0; i<nThread; i++) {
arguments[i].img2 = img2;
arguments[i].mutex = &mutex;
}
int j = 0;
for (i=0; i<(img->height)/nThread + mod; i++) {
for (j=0; j<nThread; j++) {
arguments[j].img = img; arguments[j].line = i*nThread + j;
error=pthread_create(&threads[j],NULL,threadBlur,(void*)&arguments[j]);
if(error!=0)
err(error,"pthread_create");
}
for (j=0; j<nThread; j++) {
error=pthread_join(threads[j],NULL);
if(error!=0)
err(error,"pthread_join");
}
}
free(img2);
return 0;
}
void* threadBlur(void* argument) {
// unpacking arguments
args* image = (args*)argument;
struct image* img = image->img;
struct image* img2 = image->img2;
pthread_mutex_t* mutex = image->mutex;
int error;
int line = image->line;
if (line < img->height) {
int i;
error=pthread_mutex_lock(mutex);
if(error!=0)
fprintf(stderr,"pthread_mutex_lock");
for (i=0; i<img->width; i++) {
img->pixels[line * img->width +i] = blur(img2,i,line);
}
error=pthread_mutex_unlock(mutex);
if(error!=0)
fprintf(stderr,"pthread_mutex_unlock");
}
pthread_exit(NULL);
}
struct pixel blur(struct image* img2, int x, int y) {
double red = 0;
double green = 0;
double blue = 0;
red=(double)img2->pixels[y * img2->width + x].r/5.0;
green=(double)img2->pixels[y * img2->width + x].g/5.0;
blue=(double)img2->pixels[y * img2->width + x].b/5.0;
if (x != 0) {
red+=(double)img2->pixels[y * img2->width + x - 1].r/5.0;
green+=(double)img2->pixels[y * img2->width + x - 1].g/5.0;
blue+=(double)img2->pixels[y * img2->width + x - 1].b/5.0;
} else {
red+=(double)img2->pixels[y * img2->width + x].r/5.0;
green+=(double)img2->pixels[y * img2->width + x].g/5.0;
blue+=(double)img2->pixels[y * img2->width + x].b/5.0;
}
if (x != img2->width - 1) {
red+=(double)img2->pixels[y * img2->width + x + 1].r/5.0;
green+=(double)img2->pixels[y * img2->width + x + 1].g/5.0;
blue+=(double)img2->pixels[y * img2->width + x + 1].b/5.0;
} else {
red+=(double)img2->pixels[y * img2->width + x].r/5.0;
green+=(double)img2->pixels[y * img2->width + x].g/5.0;
blue+=(double)img2->pixels[y * img2->width + x].b/5.0;
}
if (y != 0) {
red+=(double)img2->pixels[(y - 1) * img2->width + x].r/5.0;
green+=(double)img2->pixels[(y - 1) * img2->width + x].g/5.0;
blue+=(double)img2->pixels[(y - 1) * img2->width + x].b/5.0;
} else {
red+=(double)img2->pixels[y * img2->width + x].r/5.0;
green+=(double)img2->pixels[y * img2->width + x].g/5.0;
blue+=(double)img2->pixels[y * img2->width + x].b/5.0;
}
if (y != img2->height - 1) {
red+=(double)img2->pixels[(y + 1) * img2->width + x].r/5.0;
green+=(double)img2->pixels[(y + 1) * img2->width + x].g/5.0;
blue+=(double)img2->pixels[(y + 1) * img2->width + x].b/5.0;
} else {
red+=(double)img2->pixels[y * img2->width + x].r/5.0;
green+=(double)img2->pixels[y * img2->width + x].g/5.0;
blue+=(double)img2->pixels[y * img2->width + x].b/5.0;
}
struct pixel pix = {(unsigned char)round(blue),(unsigned char)round(green),(unsigned char)round(red)};
return pix;
}
编辑1:
正如@job正确猜测的那样,问题是由我的结构体的memcpy引起的(结构体被复制,但结构体内部的指针仍然指向原始结构体元素)。我还删除了互斥体(它们之所以在这里是因为我认为它们可以解决我的问题,抱歉,我的错)现在我的项目运行得非常顺利(即使我们仍然可以讨论处理速度以及使用线程的需要)。正如我所说,这是一个项目,是我 C 类的大学项目。目标是并行化我们的过滤器。所以需要线程。
谢谢!
最佳答案
好吧,这并不是一个答案,而是关于您的代码的一些观察结果:
您似乎并没有真正从程序中任何位置的多个线程访问一个特定的内存单元。所以看起来 mutices 是不需要的。
或者,也许线程确实访问相同的内存段。在这种情况下,您的程序很可能只需一个线程即可完成所有计算,效率会高得多。您应该对这种情况进行基准测试并将其与线程版本进行比较。
至少对我来说,没有明显的理由说明为什么这里需要多线程。如果您在单个线程中进行这些浮点计算,它们可能会在操作系统设法生成第二个线程之前完成。与线程创建开销时间相比,工作负载微不足道。
您当前的多线程设计存在缺陷,所有工作都在互斥锁保护的代码内进行。在互斥锁之外没有任何实际工作可以完成,因此无论您创建 1000 个线程,一次只能执行 1 个线程,其他线程将处于休眠状态等待轮到它们。
关于c - 使用互斥体时的数据竞争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15292131/
我只是有一个关于 Java 并发编程的简单问题。例如,我有一个 NxN 矩阵,矩阵的每一行都有一个对应的线程,如果每一行中的线程之间没有交互,如果多个线程同时访问和修改矩阵的单独行是否安全(或正确)?
jwplayer("myElement_0").onPlay(function(){ pausePlayers(1); alert('0 has started'); }); jwplayer("my
我在想。当我使用一个std::mutex _lock 并想依赖守卫来释放它时,我可以这样使用吗? class MyClass{ private: mutable std::mutex _loc
这种互斥模式是否像我认为的那样安全?如果是这样,你怎么调用它? lock (_lock) { if (_flag) return; else _flag = true; } try {
我有一个静态库 来访问数据库。它有一个函数 readMaximum()。 readMaximum() 从数据库中读取最大值。此函数是线程安全的(使用互斥量)。 但问题是: 有两个进程A.exe和B.e
我正在为互斥锁的逻辑而苦苦挣扎;我在这里检查 key 是否被拿走,如果没有,我们拿走它,完成后释放它;但是你能帮我看看我怎样才能有一个循环来检查 key 直到它可用吗? rdb.setnx(lockk
我正在使用 clp(fd) 编写 Prolog 程序并且我很难实现我想要的约束之一。 输出是一个整数列表(长度取决于程序另一部分的输入),其中有某些相互排斥的预定义数字对,每对中的一个数字必须在输出中
我正在尝试使用 mesos/marathon 实现一个 lizardfs 集群。为此,我创建了一些 docker 镜像。 我需要获取某种类型的 docker 容器(lizardfs-master),而
假设我有一个包含以下列的 MySQL 表: |variables|1. | 'a' |2. | 'a b' |3. | 'a b c' |4. | ... | How can
我在一个表单上有 7 个单选按钮 其中3个属于一个组,4个属于另一个组 我希望 RadioButtons 在组内互斥。 这可以通过将它们放在两个 GroupBox 中来实现, 有没有什么办法可以不把它
我正在尝试使文本输入和复选框相互排斥。 默认情况下,文本框应包含“0”,复选框应为 false。如果用户选中该复选框,则该文本框应被清空。如果用户在文本框中输入值,则应取消选中该复选框。任何时候都不应
我已经在代码的注释中写下了我的问题。 我正在尝试使用 pthreads 使我的代码并行。首先,我想通过多个线程并行地在内存中写入大量数据。写入数据后,我想通过相同的线程执行此数据。执行数据后,我想读取
这个问题在这里已经有了答案: 关闭 13 年前。 Possible Duplicate: Monitor vs Mutex in c# 你好, 在这个网站上,我发现来自不同人的不同答案令人困惑。仍然
我需要调用一个返回唯一 id 的函数, int getid() { static id=0; id++; return id; } 多个线程需要调用这个函数,我的问题是我不确定我需要在哪里锁定互斥量,
请引用来自 WWDC 的视频 https://developer.apple.com/videos/play/wwdc2015/226/演讲者展示了我们可以在两个相同类型的 NSopeation 实例
概述: 我有一个使用 CoreData 的 iOS 应用程序> 有一个名为Animal的实体 它有一组记录Lion、Tiger和Elephant 我想将只有一个记录标记为收藏。 具有相同方法的相似实体
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: DLL thread safety 你好 我正在 MS VS C++ express 中编写一个 DLL 文
我的应用程序被迫使用第 3 方模块,如果在同一台机器上同时启动两个实例,该模块将使 Windows 蓝屏。为了解决这个问题,我的 C# 应用程序有一个互斥量: static Mutex mutex =
我是 Go 的新手,我想实现一个自定义的互斥机制,每个应用程序用户一次可以执行一个 goroutine。为简化起见,将 U1 和 U2 视为应用程序用户以及 F1(userId)、F2(userId)
我需要将单选和多选的功能组合到一个控件中。具体来说,我有多种选择。第一个与其他的互斥。所以,如果我选择第一个,就需要取消选中所有其他的。如果选择了其中一个,则必须取消选中第一个(如果已选择)。其他选项
我是一名优秀的程序员,十分优秀!