Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


thread issue in c++11


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 nimrodson   Members   -  Reputation: 275

Like
0Likes
Like

Posted 24 July 2012 - 09:06 PM

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]

EDIT: I don't see the source code in my pc. I'm going to paste it here:

#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;

}

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, 24 July 2012 - 09:18 PM.


Sponsor:

#2 juggler   Members   -  Reputation: 119

Like
2Likes
Like

Posted 24 July 2012 - 09:32 PM

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.

#3 nimrodson   Members   -  Reputation: 275

Like
0Likes
Like

Posted 25 July 2012 - 07:15 AM

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.


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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS