From: Yubin Ruan <ablacktshirt@gmail.com>
To: perfbook@vger.kernel.org
Subject: [Q] how to break a concurrent program without proper use of MB
Date: Fri, 5 May 2017 21:18:21 +0800 [thread overview]
Message-ID: <20170505131821.GA13604@master> (raw)
Hi,
As mentioned in the perfbook, without proper use of memory barrier, concurrent
program will be error prone. For example, in this program:
int a=0;
int b=0;
void* T1(void* dummy)
{
a = 1;
b = 1;
return NULL;
}
void* T2(void* dummy)
{
while(0 == b)
;
assert(1 == a);
return NULL;
}
int main()
{
pthread_t threads[2] = {PTHREAD_ONCE_INIT, PTHREAD_ONCE_INIT};
pthread_create(&threads[0], NULL, T1, NULL);
pthread_create(&threads[1], NULL, T2, NULL);
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
return 0;
}
there is chances that the assertion in T2 would fail, because there is no MB used
in the program.
However, after testing it so many times, the assertion never get throwed.
Adding a loop to increase the chance:
for(int i=0; i< 500; i++){
a = b = 0;
pthread_create(&threads[0], NULL, T1, NULL);
pthread_create(&threads[1], NULL, T2, NULL);
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
}
the result is the same.
How can I make the assertion fail? Any trick?
(and I am using a X64 laptop)
Thanks,
Yubin
next reply other threads:[~2017-05-05 13:18 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-05 13:18 Yubin Ruan [this message]
2017-05-06 11:40 ` [Q] how to break a concurrent program without proper use of MB Yubin Ruan
2017-05-06 3:57 ` Paul E. McKenney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170505131821.GA13604@master \
--to=ablacktshirt@gmail.com \
--cc=perfbook@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.