【问题标题】:how do I count the amount of prime numbers each thread has found Ubuntu?如何计算每个线程找到 Ubuntu 的素数数量?
【发布时间】:2021-04-21 17:23:42
【问题描述】:

我创建了一个系统,它使用用户定义的线程数来计算从 1 到 10000 的素数我想找出每个线程找到了多少并在最后输出,而不是打印每个素数。我想出了如何计算找到的素数的总数(下面的代码),但不知道如何计算每个单独的线程。任何帮助表示赞赏。


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

int gretval = 0;
struct threadArgs
{
    int start;
    int finish;
};

void *threadMain(void *p)
{
    struct threadArgs *pargs = p;
    int i, c;
    int nstart = pargs->start, nfinish = pargs->finish;
    pthread_t tid = pthread_self();

    for (i = nstart; i < nfinish; i++)
    {
        for (c = 2; c <= i - 1; c++)
        {
            if (i % c == 0)
                break;
        }
        if (c == i)
            gretval = gretval + 1;
        //printf("thread %ld : %d\n",tid,i);
    }
    pthread_exit(&gretval);
    return 0;
}

void main(int argc, char **argv)
{
    void *pretval1;
    int retval;
    int numThreads = 0;
    int i;
    printf("how many threads would you like to use?\n");
    scanf("%d", &numThreads);
    pthread_t thrID[100];
    struct threadArgs targs[100];

    if (argc > 1)
    {
        numThreads = atoi(argv[1]);
    }
    if (numThreads > 0 && numThreads <= 100)
    {
        int chunkSize = 10000 / numThreads;
        for (i = 0; i < numThreads; i++)
        {
            targs[i].start = i * chunkSize;
            targs[i].finish = (i * chunkSize) + chunkSize;
            pthread_create(&thrID[i], NULL, threadMain, &targs[i]);
        }
        for (i = 0; i < numThreads; i++)
        {
            pthread_join(thrID[i], &pretval1);
        }
    }
    retval = *(int *)pretval1;
    printf("*pretval1 = %d\n", retval);
}

根据建议编辑代码

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct threadArgs{
    int start;
    int finish;
    int counter;
};

void *threadMain(void *p){
    struct threadArgs *pargs = p;
    int i,c
    ;
    int nstart = pargs -> start, nfinish = pargs -> finish, nCounter = ++pargs -> counter;
    pthread_t tid = pthread_self();
    
    for(i=nstart; i<nfinish; i++){
        for (c=2; c<= i-1; c++){
            if(i%c == 0)
                break;
            }
            if ( c == i)
                nCounter++;
                printf("thread %ld : %d : %d\n",tid,i,nCounter);


    }
    pthread_exit(&nCounter);
    return 0;
}

void main(int argc, char **argv){
    void *pretval1;
    int retval;
    int numThreads = 0;
    int i;
    printf("how many threads would you like to use?\n");
    scanf("%d", &numThreads);
    pthread_t thrID[100];
    struct threadArgs targs[100];
    
    if ( argc > 1){
        numThreads = atoi(argv[1]);
    }
    if (numThreads > 0 && numThreads <= 100){
        int chunkSize = 10000/numThreads;
        for (i=0; i<numThreads; i++){
            targs[i].start = i*chunkSize;
            targs[i].finish = (i*chunkSize)+chunkSize;
            targs[i].counter = 0;
            pthread_create(&thrID[i], NULL, threadMain, &targs[i]);
            }
            for (i=0; i<numThreads; i++){
                pthread_join(thrID[i], &pretval1);
            }
    }
    //nCounter = *(int *)pretval1;
    //printf("*pretval1 = %d\n", nCounter);

}

【问题讨论】:

  • 我敢打赌,threadArgs 结构中的 count 成员(最初为零)对于这项任务非常有用。
  • 所以不是 gretval 更改线程参数,而是 int nCounter -> counter 然后递增 counter 而不是 gretval 和 pthread_exit 计数器?
  • 柜台上除了pthread_exit 之外的所有东西。这是没有意义的。一旦关联的线程加入,修改后的counter 字段将在main 中用于任何targs[i]。请务必在线程启动之前将targs[i].counter = 0;startfinish 成员初始化一起放入,这样您就知道每个线程的计数器从零开始。在线程 proc 中,您可以通过 ++pargs-&gt;counter; 修改计数器,例如,如果您希望它碰撞。
  • 我修改了代码,现在的输出都很奇怪......它输出的每一个数字和它发现的数量我真的很困惑。生病用新代码编辑帖子。
  • 不要抛出代码,希望它能坚持下去。你想多了。 Look here。将线程数硬编码为 4,但您仍然可以根据需要动态读取它

标签: c ubuntu pthreads


【解决方案1】:

感谢@WhozCraig,我找到了问题的答案!发帖,这样就可以关闭了。

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

struct threadArgs{
    int start;
    int finish;
    int counter;
};

void *threadMain(void *p)
{
    struct threadArgs *pargs = p;
    //int limit;
    for (int i = pargs->start; i < pargs->finish; i++)
    {
        int c;
        for (c = 2; c < i; ++c)
        {
            if (i % c == 0)
                break;
        }

        if (c == i)
            if(pargs -> counter <= 4){
            ++pargs->counter;
          } else {
            pthread_cancel(pthread_self());
          }

    }
    pthread_exit(p);
}

int main(int argc, char **argv)
{
    int numThreads = 0;
    printf("how many threads would you like?\n");
    scanf("%d",&numThreads);

    pthread_t thrID[100];
    struct threadArgs targs[100];

    if (argc > 1)
    {
        numThreads = atoi(argv[1]);
    }

    if (numThreads > 0 && numThreads <= 100)
    {
        int chunkSize = 10000 / numThreads;
        for (int i = 0; i < numThreads; ++i)
        {
            targs[i].start = i * chunkSize;
            targs[i].finish = (i + 1) * chunkSize;
            targs[i].counter = 0;
            pthread_create(&thrID[i], NULL, threadMain, targs+i);
        }

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

                    pthread_join(thrID[i], NULL);
                    printf("Thread %d : %d primes\n", i, targs[i].counter);
        }
    }

    return EXIT_SUCCESS;
}

【讨论】:

    猜你喜欢
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    相关资源
    最近更新 更多