Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

POSIX thread problem

Status
Not open for further replies.

maluk

Programmer
Oct 12, 2002
79
SG
Hi all! I got a problem regarding POSIX threads specifically pthread_mutex_lock and pthread_mutex_unlock.

Here is the code:

Code:
#include <iostream>
#include <pthread.h>
#include <unistd.h>

using namespace std;

void* doRunA(void* arg);

class A
{
  public:
	A() : a(0) { }
	void Run()
	{
	    pthread_t thr1;
	    pthread_create(&thr1, reinterpret_cast<pthread_attr_t*>(0), doRunA, this);
	    pthread_detach(thr1);
	}

	void IncrementA()
	{
	  pthread_mutex_lock(&mut);
	  this->a++;
	  pthread_mutex_unlock(&mut);
	  // sleep(5);
	}
		  
	void ShowA()
	{
	  cout << "Value of a = " << this->a << endl;
	}
  private:
	int a;
	pthread_mutex_t mut;
};

void* doRunA(void* arg)
{
	A* pA = reinterpret_cast<A*>(arg);

	while(true)
	   pA->IncrementA();

}

int main(int argc, char** argv)
{
	A myA;

		
	myA.Run();
	while(true)
	   myA.ShowA();
		
	return 0;
}

Ok. Try running the code above.
I was expecting to see an output similar to the one below: (an incrementing value)

Code:
Value of a = 0;
Value of a = 1;
Value of a = 2;
...
...

But instead, what got are all zeros.

Code:
Value of a = 0;
Value of a = 0;
Value of a = 0;
...
...

Can someone help me out?
Thanks!

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
Ok. As a follow up, using some logging mechanism I discovered that making the mutex variable a member variable has caused the problem. It seems that the the call to pthread_mutex_lock(&mut) in A::IncrementA() never returns.

Can someone explain the theory behind this? I am really at a loss.

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
Unless ShowA() also uses the same mutex then both threads (doRunA and main) are going to run in free-run mode, which basically means they get a block of CPU time before control is passed to the other thread.

So your output would be
Code:
Value of a = 0;
Value of a = 0;
...
Value of a = 2345;
Value of a = 2345;
...
...
Value of a = 7890;
Value of a = 7890;
Basically, long runs of identical values, then a big jump to another set of identical values.

Also, how did you create the mutex?

--
 
I think if you declare a mutex as I did in the class definition, it will be created also it will be a 'fast mutex' by default so there is no need to initialize it too.

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top