我尝试将线程与条件变量同步以输出曼德博,但我得到了错误的曼德博。 函数output_mandel_line 和compute_mandel_line 已给出并且是正确的。我做了 void *function() 和 int main()。我怀疑问题出在 mutex_lock 内的 if 语句。我尝试了不同的 if 或 while 语句,但无法修复它。也许我如何使用 cond_wait ,cond_broadcast 也存在问题。
/*
* mandel.c
*
* A program to draw the Mandelbrot Set on a 256-color xterm.
*
*/
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "mandel-lib.h"
#include <pthread.h>
#include <errno.h>
#define perror_pthread(ret, msg) \
do { errno = ret; perror(msg); } while (0)
#define MANDEL_MAX_ITERATION 100000
int nthreads;
pthread_cond_t cond;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/***************************
* Compile-time parameters *
***************************/
/*
* Output at the terminal is is x_chars wide by y_chars long
*/
int y_chars = 50;
int x_chars = 90;
/*
* The part of the complex plane to be drawn:
* upper left corner is (xmin, ymax), lower right corner is (xmax, ymin)
*/
double xmin = -1.8, xmax = 1.0;
double ymin = -1.0, ymax = 1.0;
/*
* Every character in the final output is
* xstep x ystep units wide on the complex plane.
*/
double xstep;
double ystep;
/*
* This function computes a line of output
* as an array of x_char color values.
*/
void compute_mandel_line(int line, int color_val[])
{
/*
* x and y traverse the complex plane.
*/
double x, y;
int n;
int val;
/* Find out the y value corresponding to this line */
y = ymax - ystep * line;
/* and iterate for all points on this line */
for (x = xmin, n = 0; n < x_chars; x+= xstep, n++) {
/* Compute the point's color value */
val = mandel_iterations_at_point(x, y, MANDEL_MAX_ITERATION);
if (val > 255)
val = 255;
/* And store it in the color_val[] array */
val = xterm_color(val);
color_val[n] = val;
}
}
/*
* This function outputs an array of x_char color values
* to a 256-color xterm.
*/
void output_mandel_line(int fd, int color_val[])
{
int i;
char point ='@';
char newline='\n';
for (i = 0; i < x_chars; i++) {
/* Set the current color, then output the point */
set_xterm_color(fd, color_val[i]);
if (write(fd, &point, 1) != 1) {
perror("compute_and_output_mandel_line: write point");
exit(1);
}
}
/* Now that the line is done, output a newline character */
if (write(fd, &newline, 1) != 1) {
perror("compute_and_output_mandel_line: write newline");
exit(1);
}
}
void *function(void *arg){
int *number = (int*)arg, line;
int color_val[x_chars];
for(line = *number; line < y_chars; line+=nthreads){
compute_mandel_line(line,color_val);
pthread_mutex_lock(&mutex);
output_mandel_line(1,color_val);
pthread_cond_signal(&cond);
if( line + nthreads < y_chars ) {
pthread_cond_wait(&cond,&mutex);
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&cond);
}
free(arg);
return NULL;
}
int main(int argc, char **argv)
{
if(argc != 2) {
fprintf(stderr, "Wrong format");
exit(1);
}
nthreads = atoi(argv[1]);
int i;
pthread_t t[nthreads];
//pthread_t *t =(pthread_t*)malloc(nthreads * sizeof(pthread_t));
//ret =(pthread_cond_t*)malloc(nthreads * sizeof(pthread_cond_t));
int ret;
xstep = (xmax - xmin) / x_chars;
ystep = (ymax - ymin) / y_chars;
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);
for( i=0; i<nthreads; i++){
int* a = malloc(sizeof(int));
*a = i;
ret = pthread_create(&t[i], NULL , function , a);
if(ret){
perror_pthread(ret,"error");
exit(1);
}
}
for( i=0; i<nthreads; i++){
ret = pthread_join(t[i],NULL);
if(ret){
perror_pthread(ret,"error");
exit(1);
}
}
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
/*
* Allocate memory for threads and semaphores
* draw the Mandelbrot Set, one line at a time.
* Output is sent to file descriptor '1', i.e., standard output.
*/
reset_xterm_color(1);
return 0;
}
我尝试更改 if 语句和 cond_wait 、 cond_signal 的条件变量顺序
我看到我的问题中有一个错误,但是我仍然不明白如何将条件变量实现到曼德尔布罗特集中,我已经更新了前面的语句,因为我显然被误导了