c - 为什么pthreads在c中顺序执行

标签 c pthreads

我正在用 c 创建 10 个 pthread,它们似乎由于某种原因按顺序运行。我为每个线程分配了一个 id,并且为每个线程将该 id 写入数组 10 次。一旦 10 个线程完成写入,我就会解析该数组。遍历数组时,每个线程的 id 连续出现。有 100 个 id 被写入数组,并且它们都是连续写入的。我期望发生竞争条件,以便某些 id 被覆盖。我也不希望所有 id 连续出现在数组中。

代码如下:

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

//Count variabes and flags
int noItemsToProduce = 10; //Number of items produced by a single thread.
int totalItemsToProduce = 100; //Number of items produced by 10 threads.
int noItemsConsumed = 0; //Number of items removed by consumer.
int done = 0; //Flag used to know when to terminate.
int queueEmpty = 0;
void *res; //Stores the result from the thread.

//Queue Variables
int *queueArray;
int front = 0;
int back = -1;

void enqueue(int item)
{
    if(back < totalItemsToProduce-1)
    {
        queueArray[++back] = item;
    }
}

void dequeue()
{
    if(front<=back)
    {
        front++;
    }
    else
    {
        queueEmpty = 1;
        front=0;
        back=-1;
    }
}

static void *producer(void *arg)
{
    int i;
    int id = strtol(arg,NULL,0);
    for(i=0;i<noItemsToProduce;i++)
    {
        enqueue(id);
    }
}

static void *consumer(void *arg)
{
    while(!done || !queueEmpty)
    {
        printf("Dequeuing item with id = %d at pos = %d.\n",queueArray[front],front);
        dequeue();
        noItemsConsumed++;
    }
}

int main(int argc,char *argv[])
{
    //The user needs to specify the number of ints each of the 10
    //producers will produce.
    if (argc!=2)
    {
        printf("Usage: %s #-items-for-each-producer\n",argv[0]);
        return -1;
    }
    else
    {
        noItemsToProduce = strtol(argv[1],NULL,0);
        totalItemsToProduce = 10*noItemsToProduce;
        queueArray = (int *)malloc(sizeof(int)*totalItemsToProduce);
    }

    //Declarations.
    queueArray = (int *)malloc(sizeof(int)*(10*noItemsToProduce));
    pthread_t p1;
    pthread_t p2;
    pthread_t p3;
    pthread_t p4;
    pthread_t p5;
    pthread_t p6;
    pthread_t p7;
    pthread_t p8;
    pthread_t p9;
    pthread_t p10;
    pthread_t c;

    //Start producer and consumer threads.
    pthread_create(&p1,NULL,producer,"1");
    pthread_create(&p2,NULL,producer,"2");
    pthread_create(&p3,NULL,producer,"3");
    pthread_create(&p4,NULL,producer,"4");
    pthread_create(&p5,NULL,producer,"5");
    pthread_create(&p6,NULL,producer,"6");
    pthread_create(&p7,NULL,producer,"7");
    pthread_create(&p8,NULL,producer,"8");
    pthread_create(&p9,NULL,producer,"9");
    pthread_create(&p10,NULL,producer,"10");

    //Wait until all of the producers finish producing.
    pthread_join(p1,&res);
    pthread_join(p2,&res); 
    pthread_join(p3,&res); 
    pthread_join(p4,&res); 
    pthread_join(p5,&res); 
    pthread_join(p6,&res); 
    pthread_join(p7,&res); 
    pthread_join(p8,&res); 
    pthread_join(p9,&res); 
    pthread_join(p10,&res); 

    //We're done producing so let the consumer know.
    done = 1;
    pthread_create(&c, NULL, consumer, queueArray);
    pthread_join(c,&res);   
    printf("Total items produced = %d.\n",10*noItemsToProduce);
    printf("Total items consumed = %d.\n",noItemsConsumed-1);

    return 0;
}

最佳答案

你确实有竞争条件。您在操作系统的 pthreads 实现的制品中看到了什么。如果您让每个线程做更多的工作(理想情况下,做更多的工作),您可能会看到该工件消失。

关于c - 为什么pthreads在c中顺序执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14907559/

相关文章:

c - pthreads:Linux 上 gcc 和 icc 之间的不一致

c++ - pthread - 使用线程访问多个对象

c - 使用 C 访问 Berkeley DB 结构内的值

c - MinGW,始终与 pthreads 链接

c - 无限循环直到按下键

c - 多核套接字有什么好处吗? (Linux)

c - 除了 pthread_create 在 linux 上创建 linux 线程的方法

c - 当在具有 4 个线程的程序上调用 pthread_cond_signal 时,同一线程获取互斥锁

c - 再执行一次 while 循环的标准用法

c - 让 C 编译器在 OS X 上运行?