Mutex lock is not working like I want it to in Linux
I'm trying to lock two mutexes so that the output of each thread (8 threads total) doesn't mix up.The main part of the code creates these 8 threads and sets the policy to FIFO. It kinda a works but not all the thread outputs. The code underneath is the only function including any mutex of any kind in the whole code.
the code is:
void* print_message_function2( void* x )
{
ostringstream convert;
long int num = (long int) x;
long int counter = 0;
pthread_mutex_lock(&mutex1);
string ThreadId;
convert << num;
ThreadId = convert.str();
cout << "Thread " << ThreadId << " is started";
cout << endl;
pthread_mutex_unlock(&mutex1);
while(globalstop == false)
{
counter++;
}
pthread_mutex_lock(&mutex2);
string LoopCounter;
convert << counter;
LoopCounter = convert.str();
cout << "Thread "<< ThreadId <<" Looped: " << LoopCounter;
cout << endl;
pthread_mutex_unlock(&mutex2);
pthread_exit (NULL);
}
Also a sample output from the bourne shell:
Thread 1 is started
Thread 5 is started
Thread 3 is started
Thread 7 is started
Thread 1 Looped: 1185961319
c++ linux multithreading unix mutex
add a comment |
I'm trying to lock two mutexes so that the output of each thread (8 threads total) doesn't mix up.The main part of the code creates these 8 threads and sets the policy to FIFO. It kinda a works but not all the thread outputs. The code underneath is the only function including any mutex of any kind in the whole code.
the code is:
void* print_message_function2( void* x )
{
ostringstream convert;
long int num = (long int) x;
long int counter = 0;
pthread_mutex_lock(&mutex1);
string ThreadId;
convert << num;
ThreadId = convert.str();
cout << "Thread " << ThreadId << " is started";
cout << endl;
pthread_mutex_unlock(&mutex1);
while(globalstop == false)
{
counter++;
}
pthread_mutex_lock(&mutex2);
string LoopCounter;
convert << counter;
LoopCounter = convert.str();
cout << "Thread "<< ThreadId <<" Looped: " << LoopCounter;
cout << endl;
pthread_mutex_unlock(&mutex2);
pthread_exit (NULL);
}
Also a sample output from the bourne shell:
Thread 1 is started
Thread 5 is started
Thread 3 is started
Thread 7 is started
Thread 1 Looped: 1185961319
c++ linux multithreading unix mutex
When/how doesglobalstopbecome notfalse? Where/how are the mutexes initialized?
– Scott Hunter
Nov 3 '14 at 20:59
The globalstop is not relevant to problem. The globalstop becoms true when "Enter" is pressed, each thread spins around and counts up and then when enter is pressed it's suppsoed to output each threads spin-count . @ScottHunter
– MattiasLarsson
Nov 3 '14 at 21:02
And the mutex initializations?
– Scott Hunter
Nov 3 '14 at 21:06
mutex initilazion:pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;@ScottHunter
– MattiasLarsson
Nov 4 '14 at 12:46
add a comment |
I'm trying to lock two mutexes so that the output of each thread (8 threads total) doesn't mix up.The main part of the code creates these 8 threads and sets the policy to FIFO. It kinda a works but not all the thread outputs. The code underneath is the only function including any mutex of any kind in the whole code.
the code is:
void* print_message_function2( void* x )
{
ostringstream convert;
long int num = (long int) x;
long int counter = 0;
pthread_mutex_lock(&mutex1);
string ThreadId;
convert << num;
ThreadId = convert.str();
cout << "Thread " << ThreadId << " is started";
cout << endl;
pthread_mutex_unlock(&mutex1);
while(globalstop == false)
{
counter++;
}
pthread_mutex_lock(&mutex2);
string LoopCounter;
convert << counter;
LoopCounter = convert.str();
cout << "Thread "<< ThreadId <<" Looped: " << LoopCounter;
cout << endl;
pthread_mutex_unlock(&mutex2);
pthread_exit (NULL);
}
Also a sample output from the bourne shell:
Thread 1 is started
Thread 5 is started
Thread 3 is started
Thread 7 is started
Thread 1 Looped: 1185961319
c++ linux multithreading unix mutex
I'm trying to lock two mutexes so that the output of each thread (8 threads total) doesn't mix up.The main part of the code creates these 8 threads and sets the policy to FIFO. It kinda a works but not all the thread outputs. The code underneath is the only function including any mutex of any kind in the whole code.
the code is:
void* print_message_function2( void* x )
{
ostringstream convert;
long int num = (long int) x;
long int counter = 0;
pthread_mutex_lock(&mutex1);
string ThreadId;
convert << num;
ThreadId = convert.str();
cout << "Thread " << ThreadId << " is started";
cout << endl;
pthread_mutex_unlock(&mutex1);
while(globalstop == false)
{
counter++;
}
pthread_mutex_lock(&mutex2);
string LoopCounter;
convert << counter;
LoopCounter = convert.str();
cout << "Thread "<< ThreadId <<" Looped: " << LoopCounter;
cout << endl;
pthread_mutex_unlock(&mutex2);
pthread_exit (NULL);
}
Also a sample output from the bourne shell:
Thread 1 is started
Thread 5 is started
Thread 3 is started
Thread 7 is started
Thread 1 Looped: 1185961319
c++ linux multithreading unix mutex
c++ linux multithreading unix mutex
edited Nov 12 at 6:25
tripleee
88.3k12122178
88.3k12122178
asked Nov 3 '14 at 20:46
MattiasLarsson
123
123
When/how doesglobalstopbecome notfalse? Where/how are the mutexes initialized?
– Scott Hunter
Nov 3 '14 at 20:59
The globalstop is not relevant to problem. The globalstop becoms true when "Enter" is pressed, each thread spins around and counts up and then when enter is pressed it's suppsoed to output each threads spin-count . @ScottHunter
– MattiasLarsson
Nov 3 '14 at 21:02
And the mutex initializations?
– Scott Hunter
Nov 3 '14 at 21:06
mutex initilazion:pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;@ScottHunter
– MattiasLarsson
Nov 4 '14 at 12:46
add a comment |
When/how doesglobalstopbecome notfalse? Where/how are the mutexes initialized?
– Scott Hunter
Nov 3 '14 at 20:59
The globalstop is not relevant to problem. The globalstop becoms true when "Enter" is pressed, each thread spins around and counts up and then when enter is pressed it's suppsoed to output each threads spin-count . @ScottHunter
– MattiasLarsson
Nov 3 '14 at 21:02
And the mutex initializations?
– Scott Hunter
Nov 3 '14 at 21:06
mutex initilazion:pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;@ScottHunter
– MattiasLarsson
Nov 4 '14 at 12:46
When/how does
globalstop become not false? Where/how are the mutexes initialized?– Scott Hunter
Nov 3 '14 at 20:59
When/how does
globalstop become not false? Where/how are the mutexes initialized?– Scott Hunter
Nov 3 '14 at 20:59
The globalstop is not relevant to problem. The globalstop becoms true when "Enter" is pressed, each thread spins around and counts up and then when enter is pressed it's suppsoed to output each threads spin-count . @ScottHunter
– MattiasLarsson
Nov 3 '14 at 21:02
The globalstop is not relevant to problem. The globalstop becoms true when "Enter" is pressed, each thread spins around and counts up and then when enter is pressed it's suppsoed to output each threads spin-count . @ScottHunter
– MattiasLarsson
Nov 3 '14 at 21:02
And the mutex initializations?
– Scott Hunter
Nov 3 '14 at 21:06
And the mutex initializations?
– Scott Hunter
Nov 3 '14 at 21:06
mutex initilazion:
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; @ScottHunter– MattiasLarsson
Nov 4 '14 at 12:46
mutex initilazion:
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; @ScottHunter– MattiasLarsson
Nov 4 '14 at 12:46
add a comment |
1 Answer
1
active
oldest
votes
Your posted code works fine for me if I use printf instead of cout; this explains why that might be the case: Is cout synchronized/thread-safe?.
You could verify that your threads are getting to the right sections (and the problem is just with printing) by having a global counter associated w/ each mutex, and increment the associated counter in each critical section, and printing their final values once all of the threads have exited.
I changed to printf i still get the same result though. I added global counters to check if all threads get inside the mutexes, and they don´t in most cases 4 gets in the first mutex and 1 in the second. So the question then becoms: Why don´t all threads get created/go where their supposed to? @ScottHuter
– MattiasLarsson
Nov 4 '14 at 18:11
I suppose you'd need to look at how they are created; odd that 1 gets in the second without getting in the first.
– Scott Hunter
Nov 4 '14 at 19:01
I solved he first part, that is when it only created printed out that 4 threads came into the first part. I added a variable twice so that the output go manipulated after a while. But the second output probem still exists, 8 threads gos in 1 comes out... @ScottHunter
– MattiasLarsson
Nov 5 '14 at 9:10
Without seeing any of that code, you're on your own.
– Scott Hunter
Nov 5 '14 at 12:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26722919%2fmutex-lock-is-not-working-like-i-want-it-to-in-linux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your posted code works fine for me if I use printf instead of cout; this explains why that might be the case: Is cout synchronized/thread-safe?.
You could verify that your threads are getting to the right sections (and the problem is just with printing) by having a global counter associated w/ each mutex, and increment the associated counter in each critical section, and printing their final values once all of the threads have exited.
I changed to printf i still get the same result though. I added global counters to check if all threads get inside the mutexes, and they don´t in most cases 4 gets in the first mutex and 1 in the second. So the question then becoms: Why don´t all threads get created/go where their supposed to? @ScottHuter
– MattiasLarsson
Nov 4 '14 at 18:11
I suppose you'd need to look at how they are created; odd that 1 gets in the second without getting in the first.
– Scott Hunter
Nov 4 '14 at 19:01
I solved he first part, that is when it only created printed out that 4 threads came into the first part. I added a variable twice so that the output go manipulated after a while. But the second output probem still exists, 8 threads gos in 1 comes out... @ScottHunter
– MattiasLarsson
Nov 5 '14 at 9:10
Without seeing any of that code, you're on your own.
– Scott Hunter
Nov 5 '14 at 12:03
add a comment |
Your posted code works fine for me if I use printf instead of cout; this explains why that might be the case: Is cout synchronized/thread-safe?.
You could verify that your threads are getting to the right sections (and the problem is just with printing) by having a global counter associated w/ each mutex, and increment the associated counter in each critical section, and printing their final values once all of the threads have exited.
I changed to printf i still get the same result though. I added global counters to check if all threads get inside the mutexes, and they don´t in most cases 4 gets in the first mutex and 1 in the second. So the question then becoms: Why don´t all threads get created/go where their supposed to? @ScottHuter
– MattiasLarsson
Nov 4 '14 at 18:11
I suppose you'd need to look at how they are created; odd that 1 gets in the second without getting in the first.
– Scott Hunter
Nov 4 '14 at 19:01
I solved he first part, that is when it only created printed out that 4 threads came into the first part. I added a variable twice so that the output go manipulated after a while. But the second output probem still exists, 8 threads gos in 1 comes out... @ScottHunter
– MattiasLarsson
Nov 5 '14 at 9:10
Without seeing any of that code, you're on your own.
– Scott Hunter
Nov 5 '14 at 12:03
add a comment |
Your posted code works fine for me if I use printf instead of cout; this explains why that might be the case: Is cout synchronized/thread-safe?.
You could verify that your threads are getting to the right sections (and the problem is just with printing) by having a global counter associated w/ each mutex, and increment the associated counter in each critical section, and printing their final values once all of the threads have exited.
Your posted code works fine for me if I use printf instead of cout; this explains why that might be the case: Is cout synchronized/thread-safe?.
You could verify that your threads are getting to the right sections (and the problem is just with printing) by having a global counter associated w/ each mutex, and increment the associated counter in each critical section, and printing their final values once all of the threads have exited.
edited May 23 '17 at 10:33
Community♦
11
11
answered Nov 4 '14 at 17:30
Scott Hunter
33.1k64070
33.1k64070
I changed to printf i still get the same result though. I added global counters to check if all threads get inside the mutexes, and they don´t in most cases 4 gets in the first mutex and 1 in the second. So the question then becoms: Why don´t all threads get created/go where their supposed to? @ScottHuter
– MattiasLarsson
Nov 4 '14 at 18:11
I suppose you'd need to look at how they are created; odd that 1 gets in the second without getting in the first.
– Scott Hunter
Nov 4 '14 at 19:01
I solved he first part, that is when it only created printed out that 4 threads came into the first part. I added a variable twice so that the output go manipulated after a while. But the second output probem still exists, 8 threads gos in 1 comes out... @ScottHunter
– MattiasLarsson
Nov 5 '14 at 9:10
Without seeing any of that code, you're on your own.
– Scott Hunter
Nov 5 '14 at 12:03
add a comment |
I changed to printf i still get the same result though. I added global counters to check if all threads get inside the mutexes, and they don´t in most cases 4 gets in the first mutex and 1 in the second. So the question then becoms: Why don´t all threads get created/go where their supposed to? @ScottHuter
– MattiasLarsson
Nov 4 '14 at 18:11
I suppose you'd need to look at how they are created; odd that 1 gets in the second without getting in the first.
– Scott Hunter
Nov 4 '14 at 19:01
I solved he first part, that is when it only created printed out that 4 threads came into the first part. I added a variable twice so that the output go manipulated after a while. But the second output probem still exists, 8 threads gos in 1 comes out... @ScottHunter
– MattiasLarsson
Nov 5 '14 at 9:10
Without seeing any of that code, you're on your own.
– Scott Hunter
Nov 5 '14 at 12:03
I changed to printf i still get the same result though. I added global counters to check if all threads get inside the mutexes, and they don´t in most cases 4 gets in the first mutex and 1 in the second. So the question then becoms: Why don´t all threads get created/go where their supposed to? @ScottHuter
– MattiasLarsson
Nov 4 '14 at 18:11
I changed to printf i still get the same result though. I added global counters to check if all threads get inside the mutexes, and they don´t in most cases 4 gets in the first mutex and 1 in the second. So the question then becoms: Why don´t all threads get created/go where their supposed to? @ScottHuter
– MattiasLarsson
Nov 4 '14 at 18:11
I suppose you'd need to look at how they are created; odd that 1 gets in the second without getting in the first.
– Scott Hunter
Nov 4 '14 at 19:01
I suppose you'd need to look at how they are created; odd that 1 gets in the second without getting in the first.
– Scott Hunter
Nov 4 '14 at 19:01
I solved he first part, that is when it only created printed out that 4 threads came into the first part. I added a variable twice so that the output go manipulated after a while. But the second output probem still exists, 8 threads gos in 1 comes out... @ScottHunter
– MattiasLarsson
Nov 5 '14 at 9:10
I solved he first part, that is when it only created printed out that 4 threads came into the first part. I added a variable twice so that the output go manipulated after a while. But the second output probem still exists, 8 threads gos in 1 comes out... @ScottHunter
– MattiasLarsson
Nov 5 '14 at 9:10
Without seeing any of that code, you're on your own.
– Scott Hunter
Nov 5 '14 at 12:03
Without seeing any of that code, you're on your own.
– Scott Hunter
Nov 5 '14 at 12:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26722919%2fmutex-lock-is-not-working-like-i-want-it-to-in-linux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
When/how does
globalstopbecome notfalse? Where/how are the mutexes initialized?– Scott Hunter
Nov 3 '14 at 20:59
The globalstop is not relevant to problem. The globalstop becoms true when "Enter" is pressed, each thread spins around and counts up and then when enter is pressed it's suppsoed to output each threads spin-count . @ScottHunter
– MattiasLarsson
Nov 3 '14 at 21:02
And the mutex initializations?
– Scott Hunter
Nov 3 '14 at 21:06
mutex initilazion:
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;@ScottHunter– MattiasLarsson
Nov 4 '14 at 12:46