【问题标题】:How to control number of threads by using pthread_join in C?如何在 C 中使用 pthread_join 控制线程数?
【发布时间】:2012-09-11 07:19:14
【问题描述】:

我创建了一个 C 程序,它将从文本文件中读取 20000 个字符串,并将其发送到其他程序。我用了一段时间来循环这个文本文件并创建线程,将文本发送到另一个程序。 但我只希望 4 个线程工作。所以我使用了一个计数器并不断递减它,以及一个 if 条件来检查计数器,当它被设置为 1 时,我为之前的线程调用了 pthread_join。 我想先完成这 4 个线程,然后新建 4 个线程来获取新的文本文件字符串。 但它没有按我的需要工作。它仅每第 4 个线程处理 4 次。并且不会从文本文件中提取所有记录。

程序:-

int Read_record()
{
    printf("Inside Read_record()\n");
    pthread_t threads;
    int rc;
    char l_record[300];
    int thNum=4;

    while(1){
        MEMSET(g_record);

        if(fgets(g_record,300,g_r_fp)==NULL){
            printf("End of File.\n");
            break;
        }else{
            //printf("%s",g_record);
            printf("%s",g_record);
            rc = pthread_create(&threads, NULL, &Get_report, (void *)g_record);

            if (rc){
                printf("ERROR; return code from pthread_create() is %d\n", rc);
                exit(-1);
            }

            thNum--;
        }

        if(thNum==0){ 
            pthread_join(threads, NULL); 
            thNum=4;
        }
    }

    return 0;
}

输入文本文件包含:1 2 3 4 5 6 7 8 9 10 11 12 13 14

输出来了:-

Inside Read_record()
1
2
3
4
Inside Get_report, wget 4

Inside Get_report, wget 4

Inside Get_report, wget 4

Inside Get_report, wget 4

5
6
7
8
Inside Get_report, wget 8

Inside Get_report, wget 8

Inside Get_report, wget 8

Inside Get_report, wget 8

9
10
11
12
Inside Get_report, wget 12

Inside Get_report, wget 12

Inside Get_report, wget 12

Inside Get_report, wget 12

13
14
15
16
Inside Get_report, wget 16

Inside Get_report, wget 16

Inside Get_report, wget 16

Inside Get_report, wget 16

17
18
19
20
Inside Get_report, wget 20

Inside Get_report, wget 20

Inside Get_report, wget 20

Inside Get_report, wget 20

End of File.

愿望输出:-

Inside Get_report, wget 1
Inside Get_report, wget 2
Inside Get_report, wget 3
Inside Get_report, wget 4

Inside Get_report, wget 5
Inside Get_report, wget 6
Inside Get_report, wget 7
Inside Get_report, wget 8

Inside Get_report, wget 9
Inside Get_report, wget 10
Inside Get_report, wget 11
Inside Get_report, wget 12.
and so on..

请注意,我只希望在系统中创建 4 个线程。仅此而已。

【问题讨论】:

  • 你创建了四个线程,但是线程id只保存到一个变量中(每次创建线程都会覆盖),所以你只会加入最后一个线程。
  • 谢谢约阿希姆。你想让我创建 4 个不同的线程变量,然后为每个线程添加一个连接吗?像线程1。线程2。线程 3,线程 4。然后是 pthread_join(thread1,NULL), pthread_join(thread2,NULL), pthread_join(thread3,NULL), pthread_join(thread4,NULL) ?但它会是多线程吗?
  • 一个数组可能会更好,因为你可以使用例如thNum 在创建时对其进行索引。

标签: c multithreading pthreads pthread-join


【解决方案1】:

听起来您需要的是一个线程池,您可以在其中创建一个包含 4 个线程的池,并通过队列将工作传达给它们。主线程将读取队列中的文件并将作业(带有要处理的文本行的对象/结构)排入队列。线程将从队列中取出一个作业,处理该作业,然后获得另一个作业。

线程不应该在只处理一个作业后退出,而是循环直到被告知停止。

【讨论】:

    【解决方案2】:

    您的程序结构存在根本问题。这是我认为你想要的:

    1. 让 main 方法读取字符串
    2. 让 main 方法创建 4 个线程(将它们存储在数组或其他东西中)
    3. 实现某种形式的共享内存以将数据发送到 4 个线程
    4. 您可能需要某种形式的同步。
    5. 工作完成后终止并加入所有 4 个线程

    希望这能带您进入美妙的线程世界

    这实际上是另一个帖子中提到的thread pool

    我在线程上找到了this wonderful tutorial,我只查看了索引表,但似乎已经涵盖了您需要的所有内容。

    【讨论】:

    • 随便你怎么称呼它 :) 这个词是从标准库中实现这个对象的语言中借来的
    • 您的答案完全正确,但关键是它听起来与已经发布的答案非常相似(可能过于相似)。
    • 可能是,虽然恕我直言,我的回答对于入门程序员来说要好一些。而且我加个线程池模式的链接,其实完全一样^^
    • 我认为有必要提及关于共享内存队列的互斥(或任何其他同步)机制。
    • 这取决于实现。如果所有数据事务都由主线程初始化并使用不同的共享内存,则不需要锁定机制。虽然我明白你的意思,并会在一分钟内更新我的帖子。
    猜你喜欢
    • 1970-01-01
    • 2017-01-27
    • 2020-03-02
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多