pthread - threads and semaphores - help!

pthread - threads and semaphores - help!

Post by Ivor Co » Wed, 04 Apr 2001 20:37:21



Hi,
I am porting a multi-thread application to linux. The app is stable and
gives no nasties under windows and also QNX-neutrino.

I am using Red Hat purchased in December.

I am in the first stages of compilation and have some problems:

1. I am not sure if I need special compile flags for multi-thread
(reentrant)
2. the app compiles but either crashes with seg violation with no set stack
size for the threads, or the thread start is failed (1) if I set a stack
size. (app running at normal user level - not root)
3. I need a recursive semaphore and 'man' documentation seems to support
this. However the compile will not work as the recursive bits of the header
are conditional on __USE_GNU. I can't set this, it seems, unless I copy the
header and edit it (as an experiment - this can't be expected to be right.)

Can anyone advise me - I am a linux newbie - on the basics for threads in
linux. I understand threads in principle and have a working app for
win/neutrino - I need to know the basics for linux re recursive semahores
and compiler options etc for multi-thread.

(I do read the documentation, but it does not always stack!)

Cheers

Ivor Cox

 
 
 

pthread - threads and semaphores - help!

Post by Philip Armstro » Thu, 05 Apr 2001 02:59:57




>1. I am not sure if I need special compile flags for multi-thread
>(reentrant)

You need -D_REENTRANT I believe. See below.

Quote:>3. I need a recursive semaphore and 'man' documentation seems to support
>this. However the compile will not work as the recursive bits of the header
>are conditional on __USE_GNU. I can't set this, it seems, unless I copy the
>header and edit it (as an experiment - this can't be expected to be right.)

Read /usr/include/features.h

Quote:>Can anyone advise me - I am a linux newbie - on the basics for threads in
>linux. I understand threads in principle and have a working app for
>win/neutrino - I need to know the basics for linux re recursive semahores
>and compiler options etc for multi-thread.

>(I do read the documentation, but it does not always stack!)

:) The docs are of course spread in various places. However, for the
purpose of propramming threaded apps, you should check out the libc
docs on the subject. They should already be installed; try

 $ info libc

cheers,

Phil

--
http://www.kantaka.co.uk/ .oOo. public key: http://www.kantaka.co.uk/gpg.txt

 
 
 

pthread - threads and semaphores - help!

Post by Norman Blac » Fri, 06 Apr 2001 05:22:53


Quote:> 3. I need a recursive semaphore and 'man' documentation seems to support
> this. However the compile will not work as the recursive bits of the

header

There is no such thing as a recursive semaphore. Semaphores are not "owned"
by any thread. Mutexes are owned and can be recursive or not.

Did you intend to say "mutex" instead of semaphore, or what exactly do you
mean by recursive.

Quote:> 2. the app compiles but either crashes with seg violation with no set
stack
> size for the threads, or the thread start is failed (1) if I set a stack

I do not set a stack size for my threads. I just take the Linux pthreads
default, which I believe is 2MB, from a gander at the source code. What does
your pthread_create call look like.

--
Norman Black
Stony Brook Software
the reply, fubar => ix.netcom


Quote:> Hi,
> I am porting a multi-thread application to linux. The app is stable and
> gives no nasties under windows and also QNX-neutrino.

> I am using Red Hat purchased in December.

> I am in the first stages of compilation and have some problems:

> 1. I am not sure if I need special compile flags for multi-thread
> (reentrant)
> 2. the app compiles but either crashes with seg violation with no set
stack
> size for the threads, or the thread start is failed (1) if I set a stack
> size. (app running at normal user level - not root)
> 3. I need a recursive semaphore and 'man' documentation seems to support
> this. However the compile will not work as the recursive bits of the
header
> are conditional on __USE_GNU. I can't set this, it seems, unless I copy
the
> header and edit it (as an experiment - this can't be expected to be
right.)

> Can anyone advise me - I am a linux newbie - on the basics for threads in
> linux. I understand threads in principle and have a working app for
> win/neutrino - I need to know the basics for linux re recursive semahores
> and compiler options etc for multi-thread.

> (I do read the documentation, but it does not always stack!)

> Cheers

> Ivor Cox

 
 
 

pthread - threads and semaphores - help!

Post by Ivor Co » Fri, 06 Apr 2001 17:10:34


Hi Norman,

Apologies, I did mean to say recursive mutex - and I mean one that allows
the owning thread to nest calls to reserve it and releases it only when the
appropriate number of release calls have been received.

I have since found that -D_GNU_SOURCE, I think, will permit recursive
mutexes (by looking at pthread.h) but am not using it so far. I found no way
of specifying recursion with the default compile.

My programs works ok so far (a lot more modules to go yet!) using the
defaults (as you suggest) for stack and mutex_attr.

However, any thread finishing will stop the whole program. The 'man' implies
exit from the thread function given in start thread just finishes the
thread. I tried pthread_exit(0) to no avail. I suspect a simple 'error' -
perhaps you can suggest something?

Many thanks for your assistance

Ivor


> > 3. I need a recursive semaphore and 'man' documentation seems to support
> > this. However the compile will not work as the recursive bits of the
> header

> There is no such thing as a recursive semaphore. Semaphores are not
"owned"
> by any thread. Mutexes are owned and can be recursive or not.

> Did you intend to say "mutex" instead of semaphore, or what exactly do you
> mean by recursive.

> > 2. the app compiles but either crashes with seg violation with no set
> stack
> > size for the threads, or the thread start is failed (1) if I set a stack

> I do not set a stack size for my threads. I just take the Linux pthreads
> default, which I believe is 2MB, from a gander at the source code. What
does
> your pthread_create call look like.

> --
> Norman Black
> Stony Brook Software
> the reply, fubar => ix.netcom



> > Hi,
> > I am porting a multi-thread application to linux. The app is stable and
> > gives no nasties under windows and also QNX-neutrino.

> > I am using Red Hat purchased in December.

> > I am in the first stages of compilation and have some problems:

> > 1. I am not sure if I need special compile flags for multi-thread
> > (reentrant)
> > 2. the app compiles but either crashes with seg violation with no set
> stack
> > size for the threads, or the thread start is failed (1) if I set a stack
> > size. (app running at normal user level - not root)
> > 3. I need a recursive semaphore and 'man' documentation seems to support
> > this. However the compile will not work as the recursive bits of the
> header
> > are conditional on __USE_GNU. I can't set this, it seems, unless I copy
> the
> > header and edit it (as an experiment - this can't be expected to be
> right.)

> > Can anyone advise me - I am a linux newbie - on the basics for threads
in
> > linux. I understand threads in principle and have a working app for
> > win/neutrino - I need to know the basics for linux re recursive
semahores
> > and compiler options etc for multi-thread.

> > (I do read the documentation, but it does not always stack!)

> > Cheers

> > Ivor Cox

 
 
 

pthread - threads and semaphores - help!

Post by Huber » Fri, 06 Apr 2001 22:03:39



Quote:> Hi Norman,

> Apologies, I did mean to say recursive mutex - and I mean one that allows
> the owning thread to nest calls to reserve it and releases it only when
the
> appropriate number of release calls have been received.

> I have since found that -D_GNU_SOURCE, I think, will permit recursive
> mutexes (by looking at pthread.h) but am not using it so far. I found no
way
> of specifying recursion with the default compile.

Recursive Mutexes? Try this, but compile with -D_GNU_SOURCE

//#################################################
void InitializeCriticalSection(pthread_mutex_t * cs)
{
  pthread_mutexattr_t attr;
  pthread_mutexattr_init(&attr);
  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
  pthread_mutex_init(cs, &attr);
  pthread_mutexattr_destroy(&attr);

Quote:}

//#################################################
void DeleteCriticalSection(pthread_mutex_t * cs)
{
  pthread_mutex_destroy(cs);
Quote:}

//#################################################
void LeaveCriticalSection(pthread_mutex_t * cs)
{
  pthread_mutex_unlock(cs);

Quote:}

//#################################################
void EnterCriticalSection(pthread_mutex_t * cs)
{
  pthread_mutex_lock(cs);

Quote:}

Regards
Hubert
 
 
 

pthread - threads and semaphores - help!

Post by Norman Blac » Sat, 07 Apr 2001 05:33:35


Quote:> I have since found that -D_GNU_SOURCE, I think, will permit recursive
> mutexes (by looking at pthread.h) but am not using it so far. I found no

way

LinuxThreads does not default to recursive mutexes, so you will have to set
that up with mutex attributes.

Quote:> However, any thread finishing will stop the whole program. The 'man'
implies
> exit from the thread function given in start thread just finishes the
> thread. I tried pthread_exit(0) to no avail.

This is what my threads do. I just return from the thread procedure, so I
have no idea what to say about this.

--
Norman Black
Stony Brook Software
the reply, fubar => ix.netcom

 
 
 

1. Semaphore/thread mutex in multi threaded app.

I've an multithread application which uses both thread mutexes (pthread_mutex) and SystemV semaphores (semget etc.). My program does not behave properly when I use both of these. I've noticed that specially if one thread is in the sleep mode or waiting on some pthread_mutex and other thread is waiting on the semaphore (semget/semop), the semaphore state change also causes the pthread_mutex state change also.

Any comment on how to use both semaphores and pthread_mutex in a multithreaded application.

I'm running on redhat 7.0 (2.2 kernel).

Will redhat 7.1(2.4 kernel) make any difference?

Thanks

Ajit

2. Turtle Beach Sound??

3. does linux JDK 1.1.x use posix threads (pthreads) or green threads?

4. Linux 2.4.21-pre3-ac1

5. Help: Is sem.h and semaphore.h threaded?

6. AfterStep

7. Help: Document for Posix Thread(PThread) ?

8. New modem breaks PPP??! Please help

9. Are pthreads' semaphores supposed to do this or is it a Linux bug?

10. Process shared semaphores (Pthreads)

11. shared memory semaphores / pthread mutexes?

12. pthreads and semaphores

13. threads, semaphore and condition question