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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Threads

Status
Not open for further replies.
Oct 26, 2003
6
IN
Hi,

I have an application which runs on HP-UX11i ( 8 processor machine ). From my application, I am spawning 16/20 threads for doing a heavy processing job. Actualy I am threading a method of one of my classes like this:

int j = pthread_create( &threadId, NULL, CMyClass::processThrFunc, (void*)pCMyComponent );

void* CMyClass::processThrFunc( void* pParam )
{
CMyComponent* pCMyComponent = ( CMyComponent* )pParam;
pCMyComponent->StartProcessingFile( );
}
StartProcessingFile( ) method takes a file from input, and does some format conversions. After having done with the file, it simply waits, until another file name is passed to it.

while( m_bProcessHalt != SUN_TRUE )
{
while( m_bIdleStatus != SUN_TRUE )
{
sleep( 1 );
}
//Do the processing

}

This m_bIdleStatus is set from outside the class.

This works fine with thread count less than 8, and shows inconsistent behaviour when tried with 16/20( usually the program core dumps'. I am using aCC compiler and STL library, and I read from some of the articles that the STL library in HP-UX11i is not thread safe. Can I fix it that way ?

If that is the case, then how it works with 7 threads ?

Please help me by giving some tips / ( URLS to some articles ).

Thanks in advance
Jugs
 
If STL is not thread safe then I doubt if you shall be using it in an application that requires a lot of threads. It is just a coincidence that it is working. The same code might not work soem other time. So in case it is not thread safe, dont use it.
 
"It is just a coincidence that it is working."

But, Its showing consistent behaviour when thread count is less than 8( no. of CPUs ). Have you ever heard of anything like the no. of threads, an application can have in UNIX should be less than the no. of CPUs. I dont agree with this point any how, but the situation is making me believe.

" So in case it is not thread safe, dont use it."

Ok, I agree with this, But Do you think any standard implementation in STL uses static variables under the scenes ?
 
> Do you think any standard implementation in STL uses static variables under the scenes ?

I don't know about whether any STL does, but lots of implementations of the Standard library use them for returning strings and such.

I think what it means when an STL is not thread safe is that the objects aren't built to synchronize themselves between threads. So it doesn't necessarily mean you can't use them correctly in a threaded program, but you have to do any synchronization yourself.

You can use semaphores or whatever works best to synchronize.

Take a look at at the threads library. It's incomplete, but it gives some good threads programming advice, and it has some mutex classes you might want to use for synchronization.
 
Yet another platform-independent, multi-threading and synchronization library for C++ ZThreads:
See also ACE library: Thread-safe STL == Nightmare. Set mutex on every list insertion?.. Use STL in thread environment and use thorough locks as you need - that's right.
 
Thanks for the comments....

"I think what it means when an STL is not thread safe is that the objects aren't built to synchronize themselves between threads. So it doesn't necessarily mean you can't use them correctly in a threaded program, but you have to do any synchronization yourself."

But in my application, I am not sharing any of the objects across threads, and those objects that are shared have only READ acess, so that none of the threads modify their values at run time. Thats why I thought, STL library may be using static values under the hood like what is done with strtok.

B'coz otherwise I just cant see any possibilities on why it is behaving like this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top