ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello, I have been trying out an example of a multithreaded program with condition variables and mutexes, and the output varies from time to time... I cannot see why does it misbehave. Here is the program, and the outputs are followed.
> ./a.out
Counter value functionCount1: 1
Counter value functionCount1: 2
Counter value functionCount1: 3
Counter value functionCount2: 4
Counter value functionCount2: 5
Counter value functionCount2: 6
Counter value functionCount2: 7
Counter value functionCount1: 8
Counter value functionCount1: 9
Counter value functionCount1: 10
Counter value functionCount2: 11
> ./a.out
Counter value functionCount1: 1
Counter value functionCount1: 2
Counter value functionCount1: 3
Counter value functionCount2: 4
Counter value functionCount2: 5
Counter value functionCount2: 6
Counter value functionCount2: 7
Counter value functionCount2: 8
Counter value functionCount2: 9
Counter value functionCount2: 10
Counter value functionCount1: 11
in functionCount1 if count is between 3 and 6 it will wait
in functionCount2 if count is not between 3 and 6 it will signal (but also it can continue running always as pthread_cond_signal is non blocking)
From the output, between 3 and 6 functionCount1 doeesn't print anything as expected - so it is ok.
Hello, thank you for reply.
So, as I understood, as long as this condition holds "functionCount1 if count is between 3 and 6 it will wait", the behavior afterward can be arbitrary? and what is printed, after the counter is incremented past 6, it can be arbitrary, and these are not two possible outcomes?
Yes, if you don't impose any restrictions, any of the two threads can run. Because of the mutexes they will modify the value correctly, but there is no order imposed. (which thread run I mean).
The only condition imposed is that function1 will wait when count is between 3 and 6.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.