C++ a deadlock v streamoch

Stanislav Meduna stano na trillian.eunet.sk
Čtvrtek Červen 15 19:33:52 CEST 2000


Ahojte,

kolega mi poslal nasledovny programcek. Kym sa s nim
obratim na tvorcov C++ kniznice, rad by som to checkol
s miestnou komunitou, ci tu nie je nieco na prvy
pohlad zle.

Program alokuje stream, nieco do neho zapise,
seekne na zaciatok, nieco precita a viac sa o stream
nestara. Potom odstartuje druhy thread. Ten vlezie
do tellp() a zostane tam uz naveky. Mutex je tam len
aby sa nepovedalo, s tym streamom iny thread nerobi.

Vyzera to na nejaky zbludily file lock - stack dotycneho
threadu je

#0  0x400addeb in __sigsuspend (set=0xbf7ffc4c)
    at ../sysdeps/unix/sysv/linux/sigsuspend.c:48
#1  0x40024c82 in __pthread_wait_for_restart_signal (self=0xbf7ffe40)
    at pthread.c:785
#2  0x400262c2 in __pthread_lock (lock=0x8049ed8, self=0xbf7ffe40)
    at spinlock.c:68
#3  0x4002382a in __pthread_mutex_lock (mutex=0x8049ec8) at mutex.c:92
#4  0x40025c4b in __flockfile (stream=0x8049dfc) at lockfile.c:32
#5  0x400e26c8 in _IO_seekoff (fp=0x8049dfc, offset=0, dir=1, mode=2)
    at ioseekoff.c:54
#6  0x40056509 in ostream::tellp (this=0x8049e9c)
    at ../../../libio/iostream.cc:853
#7  0x8048ad8 in threadRoutine (threadArg=0x0) at streamThreads.cpp:51
#8  0x40022b85 in pthread_start_thread (arg=0xbf7ffe40) at manager.c:241

a hlavneho threadu

#0  0x400addeb in __sigsuspend (set=0xbffff478)
    at ../sysdeps/unix/sysv/linux/sigsuspend.c:48
#1  0x40024c82 in __pthread_wait_for_restart_signal (self=0x4002c940)
    at pthread.c:785
#2  0x400262c2 in __pthread_lock (lock=0x8049dc0, self=0x4002c940)
    at spinlock.c:68
#3  0x400237ba in __pthread_mutex_lock (mutex=0x8049db0) at mutex.c:84
#4  0x8048a54 in main () at streamThreads.cpp:37 

gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
Linux 2.2.15pre19

Mohli by ste to niekto vyskusat (pripadne aj na inych
architekturach, kompilatoroch, jadrach) a dat mi vediet
vysledok?

Vdaka
				Stano

=== snip ===
#include <fstream.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

fstream *myStream;
pthread_mutex_t myMutex;

void *threadRoutine(void *);
pthread_t threadId;

int exitFlag = 0;

int main()
{
  pthread_mutex_init(&myMutex, 0);

  myStream = new fstream("testfile", ios::in|ios::out);
  if (!myStream || !myStream->good())
      exit(100);

  *myStream << "Just to make some output right now ";

  // read, what I've just written
  myStream->seekg(0);

  char rdbuf[11];
  // If I comment following line out, all is OK
  myStream->read(rdbuf, 10);

  pthread_create( &threadId, 0 , threadRoutine, 0);

  int currentState = 0;
  do
  {
    sleep(1);
    pthread_mutex_lock(&myMutex);
    currentState = exitFlag;
    pthread_mutex_unlock(&myMutex);
  }
  while(! currentState);
}


void *threadRoutine(void *threadArg)
{
  pthread_mutex_lock(&myMutex);

  // This tellp deadlocks
  long currentWrOffset = myStream->tellp();

  exitFlag = 1;

  pthread_mutex_unlock(&myMutex);
}
=== snip ===



Další informace o konferenci Linux