简体   繁体   English

如何使程序以这种方式工作?

[英]How to make the program work this way?

So i have a program that does these calculations with numbers.所以我有一个程序可以用数字进行这些计算。 The program is threaded, and the number of threads are specified from the user.程序是线程化的,线程数由用户指定。

I will give a close example我将举一个紧密的例子

static void *program_thread(void *thread)
{

    bool somevar = true;

    if(somevar)
    {
        work = getwork();
    }

    dowork(work);

    if(condition1 blah blah)
        somevar = false; /* disable getwork */

    if(condition2)
       somevar = true; /* condition was either met or not met, so we request 
new work either way */  

}

Then with pthreads(and i will skip some code) i do然后用 pthreads(我会跳过一些代码)我做

int main(blah)
{
    if (pthread_create(&thr->pth, NULL, program_thread, thread_number)) {
        printf("%s","program thread create failed");
        return 1;
    }
}

Now i will start explaining.现在我将开始解释。 The number of threads created are specified from the user, so i do a for loop and create as many threads as i need.创建的线程数是由用户指定的,所以我做了一个 for 循环并根据需要创建尽可能多的线程。 Each thread calls每个线程调用

work = getwork();

Thus getting independant work to do, however the CPU is slow for this kind of job.因此可以独立完成工作,但是这种工作的 CPU 速度很慢。 It tries to compute something by trying 2^32 numbers(which is from 1 to 4 294 967 296)它试图通过尝试 2^32 个数字(从 1 到 4 294 967 296)来计算一些东西

But my CPU can only do around 3 million numbers per second, and by the time it reaches 4 billion numbers, it's restarted(for new work).但是我的 CPU 每秒只能处理大约 300 万个数字,当它达到 40 亿个数字时,它会重新启动(用于新工作)。

So i then thought of a better method.于是我想到了一个更好的方法。 Instead of each thread getting totally different work, all the threads should get the same work and split the numbers they need to try.与其让每个线程获得完全不同的工作,不如让所有线程都获得相同的工作拆分他们需要尝试的数量。

The problem is, that i can't controll what work it get's, so i must fetch问题是,我无法控制它的工作,所以我必须取

work = getwork();

Before initiating the threads.在启动线程之前。 The question is HOW?问题是如何? Using pthread_create obviously...but then what?显然使用 pthread_create ......但是然后呢?

You get more than one way to do it:你有不止一种方法来做到这一点:

  • split your work package into smaller parts (thus, your getWork returns a new, smaller work)将您的工作 package 分成更小的部分(因此,您的 getWork 返回一个新的、更小的工作)
  • store your work in a common place, that you access from your thread using a reader-writer pattern将您的工作存储在一个公共位置,您可以使用读写器模式从您的线程访问该位置
  • from the pthread API , the 4th parameter is given to your thread, you can do something like the following code:pthread API中,将第 4 个参数提供给您的线程,您可以执行以下代码:

     Work = getWork(); if (pthread_create(&thr->pth, NULL, program_thread, (void*) &work))...

    And your program_thread function would be like that而你的 program_thread function 就是这样

    static void *program_thread(void *pxThread) { Work* pWork = (Work*) pxThread; ...

    Of course, you need to check the validaty of the pointer and common stuff (in my example, I created it on stack which is most probably a bad idea).当然,您需要检查指针和常见内容的有效性(在我的示例中,我在堆栈上创建了它,这很可能是个坏主意)。 Note that your code is givig a thread_number as a pointer, which is usually a bad idea.请注意,您的代码将 thread_number 作为指针提供,这通常是一个坏主意。 If you want to have more information transfered to your thread, simply hide it into a structure.如果您想将更多信息传输到您的线程,只需将其隐藏到结构中即可。

I'm not sure I fully understood your issue, but this could give you some hints most probably.我不确定我是否完全理解您的问题,但这很可能会给您一些提示。 Please note also that when doing multithreading, you need to take into account specific issues like race conditions, concurrent access and more complex lifecycle of objects...另请注意,在执行多线程时,您需要考虑竞争条件、并发访问和更复杂的对象生命周期等特定问题......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM