Sign in to follow this  

thread issue in c++11

This topic is 1965 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I'm using the new threads features that C++11 provides, but it isn't working as i'd expect. I wonder if this trouble is because i'm doing something wrong or because c++11 still lacks of maturity.
The code:

[source lang="cpp"]
#include <thread>
#include <mutex>
#include <iostream>
#include <vector>

using namespace std;

void hello()
{
mutex m;
m.lock();
cout << "I'm a thread" << endl;
m.unlock();
}

const int N_Threads = 5;

int main()
{
vector<thread> vthreads;

for(int i=1; i<=N_Threads; i++)
vthreads.push_back(thread(hello));

for(auto &thread : vthreads)
thread.join();

return 0;
}

[/source]

[color=#ff0000]EDIT: I don't see the source code in my pc. I'm going to paste it here: [/color]

[color=#ff0000]#include <thread>
#include <mutex>
#include <iostream>
#include <vector>[/color]

[color=#ff0000]using namespace std;[/color]

[color=#ff0000]void hello()
{
mutex m;
m.lock();
cout << "I'm a thread" << endl;
m.unlock();
}[/color]

[color=#ff0000]const int N_Threads = 5;[/color]

[color=#ff0000]int main()
{
vector<thread> vthreads;

for(int i=1; i<=N_Threads; i++)
vthreads.push_back(thread(hello));

for(auto &thread : vthreads)
thread.join();

return 0;[/color]
[color=#ff0000]}[/color]

Below this, an example of the program output:

I'm a threadI'm a thread
I'm a thread
I'm a thread
I'm a thread

In a first instance, I simply ran the program without using mutexes and displayed a very similar previous output. In that moment, I supposed the "cout" object (in addition with the "<<" message) wasn't thread safe, so I put the mutex to try to prevent the interleaving, but it seems it didn't work.
What's happening?

Thank you. Edited by nimrodson

Share this post


Link to post
Share on other sites
The mutex must be shared between the threads, so move the mutex declaration out of the hello function and make it global instead. At the moment each thread has its own mutex, so locking it does nothing.

Share this post


Link to post
Share on other sites
[quote name='juggler' timestamp='1343187174' post='4962807']
The mutex must be shared between the threads, so move the mutex declaration out of the hello function and make it global instead. At the moment each thread has its own mutex, so locking it does nothing.
[/quote]

You're right, what stupid mistake I made! I need more reading on concurrency topics.
Thank you very much!

Share this post


Link to post
Share on other sites

This topic is 1965 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this