gpt4 book ai didi

c - 矩阵中的多线程搜索,行索引未正确显示

转载 作者:行者123 更新时间:2023-11-30 16:11:40 25 4
gpt4 key购买 nike

这是我在 StackOverflow 上的第一个问题。我正在尝试对 C 中的互斥体进行练习,要求我在矩阵 N x N 中查找一个元素。每个线程都必须检查该元素的每一行。当线程找到该元素时,它应该告诉其他线程,以便他们可以退出而不继续搜索。我已经编写了一个解决方案,使用标志来检查是否已找到该元素,但无法弄清楚为什么行索引未正确显示。我认为矩阵中的内存地址是连续的,所以我认为使用 % 操作数可以帮助我。我也不确定线程​​函数是否正确编写。您能帮我找出为什么行索引未正确显示吗?还有其他优雅的解决方案吗?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define MAXNTHREADS 1000000
#define MIN(a,b) (a < b ? a : b)

void * search (void *);
int nthreads;
char tosearch;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int flag;

int main (int argc, char **argv)
{
pthread_t * threads;
char * matrix;

if (argc != 3)
{
fprintf (stderr, "usage: matrix <char> <#threads>\n");
exit (-1);
}

nthreads = MIN (atoi(argv[2]), MAXNTHREADS);
tosearch = argv[1][0];

matrix = malloc (nthreads * nthreads);
threads = malloc (sizeof (pthread_t) * nthreads);

printf ("Insert %d elements\n", nthreads * nthreads);

for (int i = 0; i < nthreads; i ++)
{
for (int j = 0; j < nthreads; j ++)
{
scanf ("%c", matrix + i * nthreads + j);
getchar();
}
}

printf ("MATRIX:\n");
for (int i = 0; i < nthreads; i ++)
{
for (int j = 0; j < nthreads; j ++)
{
printf ("%c\t", *(matrix + i * nthreads + j));
}

printf ("\n");
}



for (int i = 0; i < nthreads; i ++)
{
pthread_create (threads + i, NULL, search, matrix + i * nthreads);
}

for (int i = 0; i < nthreads; i ++)
{
pthread_join (* (threads + i), NULL);
}

free (matrix);
free (threads);

exit (0);
}

void * search (void *arg)
{
printf ("Process %ld started\n", pthread_self());

for (int i = 0; i < nthreads; i ++)
{

pthread_mutex_lock (&mutex);
if (flag == 0)
{
if (*((char *)arg + i) == tosearch)
{
printf ("Element found by thread %ld in position %ld %d\n", pthread_self(), (((unsigned long)arg + nthreads) % nthreads), i);
flag = 1;
pthread_mutex_unlock (&mutex);
return NULL;
}

pthread_mutex_unlock (&mutex);
}
else
{
pthread_mutex_unlock (&mutex);
printf ("Process %ld exits\n", pthread_self());
pthread_exit (NULL);
}
}

printf ("Process %ld, found nothing\n", pthread_self());
pthread_exit (NULL);
}

最佳答案

我通过将指向矩阵的指针存储为全局变量来解决索引问题。然后我将矩阵起始地址减去元素地址并将结果除以 nthreads。这样我就有了元素的正确行索引。不管怎样,我不确定这是否是解决这个问题的优雅解决方案。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define MAXNTHREADS 1000000
#define MIN(a,b) (a < b ? a : b)

void * search (void *);
int nthreads;
char tosearch;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int flag;
char *matrix;

然后在函数搜索中:

void * search (void *arg)
{
printf ("Process %ld started\n", pthread_self());

for (int i = 0; i < nthreads; i ++)
{

pthread_mutex_lock (&mutex);
if (flag == 0)
{
if (*((char *)arg + i) == tosearch)
{
printf ("Element found by thread %ld in position %ld %d\n", pthread_self(), (((unsigned long)arg + i) - (unsigned long)matrix) / nthreads, i);
flag = 1;
pthread_mutex_unlock (&mutex);
return NULL;
}

pthread_mutex_unlock (&mutex);
}
else
{
pthread_mutex_unlock (&mutex);
printf ("Process %ld exits\n", pthread_self());
pthread_exit (NULL);
}
}

printf ("Process %ld, found nothing\n", pthread_self());
pthread_exit (NULL);
}

关于c - 矩阵中的多线程搜索,行索引未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58550920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com