我在c中创建10个pthreads,似乎它们由于某种原因顺序运行。我为每个线程分配了一个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;
}
答案 0 :(得分:2)
pthreads
对如何安排线程做出零保证。如果他们没有做很多工作,操作系统可能会按顺序安排每个线程并让它们完成。
如果你真的想让他们参加比赛,那就让他们做一些像for(i=0; i<100000000; i++) write_to_array;
这样繁忙的工作。当然,即使这并不能保证他们真的会参加比赛,但它确实可以提高大多数调度员的胜算。
答案 1 :(得分:2)
你确实有竞争条件。您在操作系统的pthreads实现的人工制品中看到了什么。如果你让每个线程做更多的工作(理想情况下,更多的工作),你可能会看到人工制品消失。
答案 2 :(得分:0)
问题是什么?
&#34;问题&#34;是这样的:
我也不希望所有的ID都连续出现在数组中。
予。即你有期望和/或对实施细节做出假设。
答案 3 :(得分:0)
如果你要求四个弓箭手各自得到他们的弓箭,前往战场,在目标上排队,然后射击,你可能同时在空中有两个箭,但这不太可能。大部分时间都花在创建和拆除线程上。两个将同时访问阵列的概率几乎为零。