Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2007-05

POSIX threads and classes

9 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Артем Смирнов

I've got some troubles with pthread_cond_init when calling it in class members. After this action next calling of class constructor for second object bring to SIGSEGV. When I tried to use Object instead Class all seems fine, but for my task Object not approach. What it can be?

program pthcondtest;
{$mode objfpc}{$H+}
uses
  pthreads;
type
TCondv = class
   CAttr : pthread_condattr_t;
   Cond : pthread_cond_t;
public
  constructor Create;
  destructor Destroy; override;
end;
{TCondv}
constructor TCondv.Create;
begin
  pthread_condattr_init(CAttr);
  pthread_cond_init(Cond, CAttr);
end;
destructor TCondv.Destroy;
begin
  inherited;
  pthread_cond_destroy(Cond);
  pthread_condattr_destroy(CAttr);
end;
{TCondv}
var
  obj1 : TCondv;
  obj2 : TCondv;
begin
  obj1 := TCondv.Create;
  obj2 := TCondv.Create;
  obj1.Destroy;
  obj2.Destroy;
end.

Reply by email

Leonardo M. Ram�
to Артем Смирнов
Show quoted text 6 lines
{TCondv} constructor TCondv.Create; begin pthread_condattr_init(CAttr); pthread_cond_init(Cond, CAttr); end;

I never used pthreads, but looking at the constructor of TCondv, it seems to require CAttr and Conv to be setted before calling pthread_condattr_init and pthread_cond_init.

Leonardo M. Ramé
http://leonardorame.blogspot.com

__________________________________________________ Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

Reply by email

Артем Смирнов
to Leonardo M. Ram�

No, pthread_condattr_init and pthread_cond_init initialize variables CAttr and Cond. By the by, this code written in C work right.

2007/5/7, Leonardo M. Ramé :

Show quoted text 13 lines
> {TCondv} > constructor TCondv.Create; > begin > pthread_condattr_init(CAttr); > pthread_cond_init(Cond, CAttr); > end; I never used pthreads, but looking at the constructor of TCondv, it seems to require CAttr and Conv to be setted before calling pthread_condattr_init and pthread_cond_init.

Reply by email

Bram Kuijvenhoven
to Артем Смирнов: I've got some troubles with pthread_cond_init when calling it in class members. After…
Артем Смирнов wrote
I've got some troubles with pthread_cond_init when calling it in class members. After this action next calling of class constructor for second object bring to SIGSEGV. When I tried to use Object instead Class all seems fine, but for my task Object not approach. What it can be?

Please specify your OS and FreePascal version. Your program works fine on my config (FPC 2.0.4, Debian Sarge, libc6 package version 2.3.2.ds1-22sarge6 (pthreads is part of the libc6 package)).

Bram

Reply by email

Артем Смирнов
to Bram Kuijvenhoven

fpc 2.0.4, opensuse 10.2, glibc-2.4-31.1

2007/5/7, Bram Kuijvenhoven :

Show quoted text 13 lines
Артем Смирнов wrote: > I've got some troubles with pthread_cond_init when calling it in class > members. After this action next calling of class constructor for second > object bring to SIGSEGV. When I tried to use Object instead Class all > seems fine, but for my task Object not approach. What it can be? Please specify your OS and FreePascal version. Your program works fine on my config (FPC 2.0.4, Debian Sarge, libc6 package version 2.3.2.ds1-22sarge6 (pthreads is part of the libc6 package)). Bram _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Helmut Hartl
> fpc 2.0.4, opensuse 10.2, glibc-2.4-31.1

You may try this version of your program
-----
program Project1;

{$mode objfpc}{$H+}
uses
 UnixType,pthreads;
type
TCondv = class
private
   Cond : UnixType.pthread_cond_t;
   CAttr : UnixType.pthread_condattr_t;
public
  constructor Create;
  destructor Destroy; override;
end;
{TCondv}
constructor TCondv.Create;
begin
  pthread_condattr_init(@CAttr);
  pthread_cond_init(@Cond, @CAttr);
end;
destructor TCondv.Destroy;
begin
  inherited;
  pthread_cond_destroy(@Cond);
  pthread_condattr_destroy(@CAttr);
end;
{TCondv}
var
  obj1 : TCondv;
  obj2 : TCondv;
begin
  obj1 := TCondv.Create;
  obj2 := TCondv.Create;
  obj1.Destroy;
  obj2.Destroy;
end.
---------------

I had bad experiences with types differently declared in pthreads and UnixTypes.
Namely the following:

In pthread:

  pthread_cond_t = record
    __c_lock: _pthread_fastlock;
    __c_waiting: _pthread_descr;
  end;

In UnixType: (ptypes.inc)

  pthread_cond_t = record
    __c_lock: _pthread_fastlock;
    __c_waiting: pointer;
    __padding:
array[0..48-1-sizeof(_pthread_fastlock)-sizeof(pointer)-sizeof(clonglong
)] of byte;
    __align: clonglong;
  end;
A call like: i:=pthread_cond_timedwait(@FSignal, @MMutex,@tim);
Fails with the first version (EINVAL), but succedes with the second one.

HTH,
helmut

Reply by email

Артем Смирнов
to Helmut Hartl: You may try this version of your program ----- program Project1; {$mode objfpc}{$H+} uses…

Yeah, this one work correctly. Thanks!

2007/5/8, Helmut Hartl :

Show quoted text 84 lines
> > fpc 2.0.4, opensuse 10.2, glibc-2.4-31.1 You may try this version of your program ----- program Project1; {$mode objfpc}{$H+} uses UnixType,pthreads; type TCondv = class private Cond : UnixType.pthread_cond_t; CAttr : UnixType.pthread_condattr_t; public constructor Create; destructor Destroy; override; end; {TCondv} constructor TCondv.Create; begin pthread_condattr_init(@CAttr); pthread_cond_init(@Cond, @CAttr); end; destructor TCondv.Destroy; begin inherited; pthread_cond_destroy(@Cond); pthread_condattr_destroy(@CAttr); end; {TCondv} var obj1 : TCondv; obj2 : TCondv; begin obj1 := TCondv.Create; obj2 := TCondv.Create; obj1.Destroy; obj2.Destroy; end. --------------- I had bad experiences with types differently declared in pthreads and UnixTypes. Namely the following: In pthread: pthread_cond_t = record __c_lock: _pthread_fastlock; __c_waiting: _pthread_descr; end; In UnixType: (ptypes.inc) pthread_cond_t = record __c_lock: _pthread_fastlock; __c_waiting: pointer; __padding: array[0..48-1-sizeof(_pthread_fastlock)-sizeof(pointer)-sizeof(clonglong )] of byte; __align: clonglong; end; A call like: i:=pthread_cond_timedwait(@FSignal, @MMutex,@tim); Fails with the first version (EINVAL), but succedes with the second one. HTH, helmut _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email