English 中文(简体)
read辛的障碍
原标题:Barriers for thread syncing

I m 制造 n子;然后在屏障破碎后开始执行。

全球数据空间:

int bkdown = 0;

主要():

pthread_barrier_init(&bar,NULL,n);

for(i=0;i<n;i++)
{
pthread_create(&threadIdArray[i],NULL,runner,NULL);
if(i==n-2)printf("breakdown imminent!
");
if(i==n-1)printf("breakdown already occurred!
");
}

校对功能:

void *runner(void *param)
{
 pthread_barrier_wait(&bar);

 if(bkdown==0){bkdown=1;printf("barrier broken down!
");}

        ...

 pthread_exit(NULL);
}

<>Expected order:

breakdown imminent!
barrier broken down!
breakdown already occurred!

<<>strong>Actual Order:(经多次测试)。

breakdown imminent!
breakdown already occurred!
barrier broken down!!

是否有人解释为什么我没有在<条码>上贴上“<>条/条码>的信息?

最佳回答
for(i=0;i<n;i++)
{
pthread_create(&threadIdArray[i],NULL,runner,NULL);
if(i==n-2)printf("breakdown imminent!
");
if(i==n-1)printf("breakdown already occurred!
");
}

直到为止,预留金刚开始燃烧。 它没有等待它开始或结束。 因此,你听从了时间表,该时间表可能决定继续实施 your,或转向新建立的read(或两者兼而有的)。

您还重新定位了n的障碍,因此,无论如何,在你创建所有屏障之前,任何透镜都不会接过。

问题回答

校对的顺序取决于操作系统。 仅仅因为你开始直线工作,就意味着本组织将立即投入运作。

如果你真的想要控制read子的交付顺序,你就必须在那里进行某种同步(有变数或条件变量)。

除了 no子和星座的答复外,你必须考虑到你在经常被忽略的法典中还有另一个序列化:你在相同的<代码>FILE变量,即<代码>stdin上填写国际交易日志。

内部可互换使用这一变量,以及<代码>n+1的顺序。 read(包括你的read子)可以接触到这一mut子,其定义是执行,基本上视你的情况是随机的。

因此,你获得<条码>f产出的顺序是,你的read通过这些虫.。

你们可以通过两种方式获得预期秩序。

  • Create each thread with a higher priority than the main thread. This will ensure that new thread will run immediately after creation and wait on the barrier.
  • Move the "breakdown imminent! " print before the pthread_create() and call use a sched_yield() call after every pthread_create(). This will schedule the newly created thread for execution.




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...