UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2006-12-06 01:35 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: compiling threads in AIX

Can I compile a program that uses POSIX threads in AIX using g++?(3.3.2)
When I try (f.e. g++ -c threadsTst.cpp -pthread (even with -D_XOPEN_SORCE=600(I even tried add as the second option -U_AIX_PTHREADS_D7))).I got pthread_rwlock_t wat not declared in this scope and other(pthread_rwlock_attr_t,pthread_spinlock_t,pthread_barrier_attr_t).

Got one more problem.When I put a static member in a class I get undefined symbol class<T>::m_member while linking.

Offline

#2 2006-12-10 01:07 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: compiling threads in AIX

It should compile pthread programs... check your library paths...

If the pthreads directory isn't included try adding it on the gcc command line via -L/usr/lib/threads and see if that helps...

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2006-12-11 08:30 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: compiling threads in AIX

I had tried this and tried again - nothing. (
Anyawy I'll do complete program on RH Linux.(There i have no probelm (neither with threads nor with static members of template class (had problems while linking as udefined symbol)))

Offline

#4 2006-12-11 05:31 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: compiling threads in AIX

Im not certain about the compiler problems...

If the paths are correct ( btw - that path should be the first in the list - if its placed elsewhere u can still have problems )... do  you have the AIX compilers (  cc_r or xlc_r ) and what happens when you try using them?

Oh... are you certain your gcc version and AIX version are matched pairs?  I remember AIX, Solaris and other having issues if the compiler / os versions weren't fully matched...

Right now I am guessing a bit since I do not have access to an AIX system to test with... :-/

For you static var problem... is it raw or wrapped ?  By wrapped I mean something like:

See example in msg below... instead of using T to represent a specialization... I wrote out the example...

This protects you from things like initialization order issues... and also gets rid of ugly code like:

template<class T> class Foo
{
	static T Bar;
};

follow by ( for example ) a

int Foo<int>::Bar = 0;

Where if the second section isn't in a header you can end up with compiler issues previously mentioned about splitting template definition and implementation between a *.h and *.cpp files...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#5 2006-12-12 08:37 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: compiling threads in AIX

About the pthreads problem...

Have you tried anything silly like prefixing the calls with :: to force id'ing them to the compiler as being in the standard namespace?


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#6 2006-12-13 09:11 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: compiling threads in AIX

I will try to implement each your advice(in each thread ) ).But firstly i want to wirte some class working with sysV shared memory.Nad have a problem.I'll post it to another thread,of course.

Offline

#7 2006-12-15 04:53 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: compiling threads in AIX

Ok... so I got some anonymous 'hate' mail the other day in my normal email... due to the example function I wrote in an earlier message...

The reason is that yes - the example didn't work... it was meant as pseudo code to convey the idea... since that failed lets try a very short BUT complete example:

#include <iostream>

template < typename T >
class Foo
{
  public:
		
    T & bar( )
    {
	// This will cause compile failure if a Foo used
	// without specialization...   i.e. no return ref
    }

    // other functions using T but not requiring specialization
};

template < >
class Foo< int >
{
  public:

    int & bar( )
    {
        static int barVal = 1;

        std::cout << "Foo< int >.bar( ) specialization with barVal: " << barVal << std::endl;
			
        return barVal;
    }
};

int main( )
{
  Foo< int  > fi;
  fi.bar( );

  // This would be compile time error - no specialization
  // Foo< char > fc;
	return 0;
}

So here is the deal... this is called class template specialization... a few things to note:

*) The Foo template's bar function does NOT return a reference value even though it declares it will do such - this means without further specialization of the class for a desired type, compilation will fail...

*) You could avoid that type of compilation failure by declaring a member variable and sending that back from the template bar( ) but why?  If you wanted to enforce the use of numeric types only as the input type then maybe it would be worth it but otherwise...

*) It DOES take more code... you cannot just say Foo<int> but instead have to write out the specialization first... so that is a downside...

*) It doesn't take THAT MUCH CODE... I could squish the shown specialization into a couple lines if I wanted...

*) This example ( where the question was about having a static / class level member being returned from and based on the type of a template ) avoids initialization order problems and so forth...

*) There are other ways to do this sort of thing... this is just want came to mind when I saw the original question...

*) - Additional / Edited - to use the bar( ) func in this example you need to instantiate a Foo< int > object prior to use - you could just as easily declare the bar( ) func to be static...  see msg below...

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#8 2006-12-15 11:09 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: compiling threads in AIX

template < >
class Foo< int >
{
  public:

    int & bar( )
    {
        static int barVal = 1;

        std::cout << "Foo< int >.bar( ) specialization with barVal: " << barVal << std::endl;
			
        return barVal;
    }
};

Offline

#9 2006-12-15 05:02 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: compiling threads in AIX

// File Foo.h

class Foo
{
  public:
   
    static int x;
};

// File Foo.cpp 

int Foo::x = [ int value or expression ];

"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#10 2006-12-16 11:18 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: compiling threads in AIX

Offline

#11 2006-12-16 11:19 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: compiling threads in AIX

Waiting for yurgy's complaints...

Lol...

Sorry - just saw u posting n was bored...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#12 2006-12-16 11:27 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: compiling threads in AIX

Guess I was too slow... oops... :(

YES and NO...

The YES is if you do it the way I mention it should work on ALL modern compilers.... its 'long'...  I know..

The NO... which I will not tell you... works on ( maybe ) 50% of the compilers....  its 'short' but hey - its BROKEN...

And not gonna post something that can break 50% of the time... sry :(



Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

Board footer

Powered by FluxBB