IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Multithreading Discussion :

crash multithread


Sujet :

Multithreading

  1. #1
    Membre régulier
    Avatar de alpha_one_x86
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    411
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Somme (Picardie)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2006
    Messages : 411
    Points : 113
    Points
    113
    Par défaut crash multithread
    Bonjour, voila mon application complete:
    http://files.first-world.info/ultrac...er-src.tar.bz2
    Et voila le code à probléme:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    /***************************************************************************
                                      WriteThread.cpp
                                  -------------------
     
         Begin        : Web May 6 2009 19:11 alpha_one_x86
         Project      : Ultracopier
         Email        : alpha.super-one@laposte.net
         Note         : See README for copyright and developer
         Target       : Define the class of the writeThread
     
    ****************************************************************************/
     
    #include "WriteThread.h"
     
    WriteThread::WriteThread(copyThread *parent)
    {
    	this->parent=parent;
    	theCurrentStat=WriteThread::Stopped;
    	stopIt=false;
    	endOfSource=false;
    	source=NULL;
    	destination=NULL;
    	theItem.id=0;
    	movingMode=false;
    	mutexWaitControlR.lock();
    	mutexWaitControlW.lock();
    	debugRcount=0;
    	debugWcount=0;
    }
     
    WriteThread::~WriteThread()
    {
    	mutexWaitControlW.unlock();
    	mutexWaitControlR.unlock();
    }
     
    void WriteThread::setFiles(QFile * source,QFile * destination,copyItemInternal theItem)
    {
    	DEBUGCONSOLE(50,"WriteThread::setFiles","source: "+source->fileName()+", destination: "+destination->fileName());
    	this->source=source;
    	this->destination=destination;
    	this->theItem=theItem;
    	controlMutexR.wakeAll();
    }
     
    void WriteThread::stop()
    {
    	stopIt=true;
    	controlMutexR.wakeAll();
    	controlMutexW.wakeAll();
    }
     
    void WriteThread::addNewBlock(QByteArray newBlock)
    {
    	DEBUGCONSOLE(50,"WriteThread::addNewBlock","newBlock size: "+QString::number(newBlock.size()));
    	debugRcount+=newBlock.size();
    	//wait if list is full
    	if(theBlockList.size()>LISTBLOCKSIZE)
    		controlMutexW.wait(&mutexWaitControlW);
    	{
    		QMutexLocker lock_mutex(&pipe);
    		theBlockList.append(newBlock);
    	}
    	//wake if list is becoming not empty
    	controlMutexR.wakeAll();
    }
     
    /// \brief end of source detected
    void WriteThread::endOfSourceDetected()
    {
    	endOfSource=true;
    	controlMutexR.wakeAll();
    }
     
    /// \brief set action on error
    void WriteThread::errorAction(int action)
    {
    	DEBUGCONSOLE(50,"WriteThread::fileExistsAction","action: "+QString::number(action));
    	actionAfterUnlock=action;
    	if(action==ERRORACTION_CLOSE)
    		stop();
    	else
    	{
    		controlMutexR.wakeAll();
    		theCurrentStat=WriteThread::Running;
    	}
    }
     
    /// \brief return the statut
    WriteThread::currentStat WriteThread::getCurrentStat()
    {
    	return theCurrentStat;
    }
     
    void WriteThread::setKeepDate(bool keepDate)
    {
    	this->keepDate=keepDate;
    }
     
    void WriteThread::setMovingMode(bool movingMode)
    {
    	this->movingMode=movingMode;
    }
     
     
    void WriteThread::run()
    {
    	DEBUGCONSOLE(50,"WriteThread::run","start");
    	if(source==NULL || destination==NULL || theItem.id==0)
    	{
    		DEBUGCONSOLE(10,"WriteThread::run","item not loaded!");
    		return;
    	}
    	do
    	{
    		endOfSource=false;
    		theCurrentStat=WriteThread::Running;
    		QByteArray blockArray;
    		qint64 positionInDestination=0,bytesWriten=0;
    		DEBUGCONSOLE(50,"WriteThread::run","start loop");
    		do
    		{
    			//wait if list is empty then wait
    			if(theBlockList.size()==0 && !endOfSource)
    				controlMutexR.wait(&mutexWaitControlR);
     
    			{
    				QMutexLocker lock_mutex(&pipe);
    				//read one block
    				if(theBlockList.size()==0)
    					blockArray.clear();
    				else
    				{
    					blockArray=theBlockList.first();
    					theBlockList.removeFirst();
    				}
    			}
    			controlMutexW.wakeAll();
     
    			//write one block
    			if(!stopIt && !blockArray.isEmpty())
    			{
    				positionInDestination=destination->pos();
    				retryWriteThisBlock:
    				bytesWriten=destination->write(blockArray);
    				if(destination->error()!=QFile::NoError || bytesWriten!=blockArray.size())
    				{
    					retrySeekDestination:
    					actionAfterUnlock=-1;
    					DEBUGCONSOLE(90,"WriteThread::run","Error in writing: "+destination->errorString());
    					emit errorOnFile(ERROR_DEF_ALL,destination->fileName(),tr("Error in writing destination: ")+destination->errorString(),theItem.id);
    					theCurrentStat=WriteThread::Paused;
    					if(actionAfterUnlock==-1)
    						controlMutexR.wait(&mutexWaitControlR);
    					theCurrentStat=WriteThread::Running;
    					if(actionAfterUnlock==ERRORACTION_RETRY)
    					{
    						if(!destination->seek(positionInDestination))
    							goto retrySeekDestination;
    						goto retryWriteThisBlock;
    					}
    					else if(actionAfterUnlock==ERRORACTION_SKIP)
    						goto SkipFile;
    					else if(actionAfterUnlock==ERRORACTION_CLOSE)
    						goto SkipFile;
    					else
    					{
    						DEBUGCONSOLE(10,"WriteThread::run","Unknow action at destination write error ("+QString::number(actionAfterUnlock)+")");
    						goto SkipFile;
    					}
    				}
    			}
    		}
    		while(!blockArray.isEmpty() && bytesWriten==blockArray.size() && !stopIt && (theBlockList.size() || !endOfSource));
    		DEBUGCONSOLE(70,"WriteThread::run","blockArray.isEmpty(): "+QString::number(blockArray.isEmpty()));
    		DEBUGCONSOLE(70,"WriteThread::run","bytesWriten: "+QString::number(bytesWriten));
    		DEBUGCONSOLE(70,"WriteThread::run","blockArray.size(): "+QString::number(blockArray.size()));
    		DEBUGCONSOLE(70,"WriteThread::run","stopIt: "+QString::number(stopIt));
    		DEBUGCONSOLE(70,"WriteThread::run","theBlockList.size(): "+QString::number(theBlockList.size()));
    		DEBUGCONSOLE(70,"WriteThread::run","endOfSource: "+QString::number(endOfSource));
    		endOfSource=false;
     
    		DEBUGCONSOLE(90,"WriteThread::run","closing source done");
    		if(destination->isOpen())
    			destination->close();
    		DEBUGCONSOLE(90,"WriteThread::run","closing destination done");
     
    		//set the time if no write thread used
    		if(keepDate)
    		{
    			retrySetDate:
    			DEBUGCONSOLE(90,"WriteThread::run","set date...");
    			if(!changeFileDateTime(destination->fileName(),source->fileName()))
    			{
    				actionAfterUnlock=-1;
    				DEBUGCONSOLE(90,"WriteThread::run","Date cannot be modified!");
    				emit errorOnFile(ERROR_DEF_ALL,source->fileName(),tr("Date cannot be modified!"),theItem.id);
    				theCurrentStat=WriteThread::Paused;
    				if(actionAfterUnlock==-1)
    					controlMutexR.wait(&mutexWaitControlR);
    				theCurrentStat=WriteThread::Running;
    				if(actionAfterUnlock==ERRORACTION_RETRY)
    					goto retrySetDate;
    				else if(actionAfterUnlock==ERRORACTION_SKIP)
    					goto SkipFile;
    				else if(actionAfterUnlock==ERRORACTION_CLOSE)
    					goto SkipFile;
    				else
    				{
    					DEBUGCONSOLE(10,"WriteThread::run","Unknow action at change file time ("+QString::number(actionAfterUnlock)+")");
    					goto SkipFile;
    				}
    			}
    			DEBUGCONSOLE(90,"WriteThread::run","set date done");
    		}
     
    		//if query to stop, directly go to SkipFile
    		if(stopIt)
    			goto SkipFile;
     
    		//remove source in moving mode
    		if(movingMode)
    		{
    			DEBUGCONSOLE(90,"WriteThread::run","try remove source...");
    			if(!source->remove())
    			{
    				actionAfterUnlock=-1;
    				DEBUGCONSOLE(10,"WriteThread::run","source->errorString():\""+source->errorString()+"\" while removing the source");
    				emit errorOnFile(ERROR_DEF_ALL,destination->fileName(),tr("Unable to remove the source file in moving mode: ")+source->errorString(),theItem.id);
    				theCurrentStat=WriteThread::Paused;
    				if(actionAfterUnlock==-1)
    					controlMutexR.wait(&mutexWaitControlR);
    				theCurrentStat=WriteThread::Running;
    				if(actionAfterUnlock==ERRORACTION_RETRY)
    					goto retrySetDate;
    				else if(actionAfterUnlock==ERRORACTION_SKIP)
    					goto SkipFile;
    				else if(actionAfterUnlock==ERRORACTION_CLOSE)
    					goto SkipFile;
    				else
    				{
    					DEBUGCONSOLE(10,"WriteThread::run","Unknow action at remove source file ("+QString::number(actionAfterUnlock)+")");
    					goto SkipFile;
    				}
    			}
    			DEBUGCONSOLE(90,"WriteThread::run","try remove source done");
    		}
     
    		DEBUGCONSOLE(90,"WriteThread::run","now is in SkipFile point!");
    		SkipFile:
    		//close file
    		DEBUGCONSOLE(90,"WriteThread::run","closing source and destination...");
    		if(destination->isOpen())
    			destination->close();
    		DEBUGCONSOLE(90,"WriteThread::run","closing destination done");
     
    		if((actionAfterUnlock==ERRORACTION_SKIP || actionAfterUnlock==ERRORACTION_CLOSE || stopIt) && destination->exists())
    		{
    			DEBUGCONSOLE(50,"WriteThread::run","try remove destination: "+destination->fileName());
    			destination->remove();
    		}
    		parent->deleteFileObject(source,destination);
    		source=NULL;
    		destination=NULL;
    		theItem.id=0;
    		theCurrentStat=WriteThread::Stopped;
    		controlMutexR.wait(&mutexWaitControlR);
    	} while(true);
    }
    Quand j'essaye de faire les wakeAll(); que quand nécessaire j'ai un deadlock, et en l'état j'ai un crash de l'application avec comme backtrace:
    ASSERT failure in QMutex::unlock(): "A mutex must be unlocked in the same thread that locked it.", file thread/qmutex.cpp, line 373

    Program received signal SIGABRT, Aborted.
    [Switching to Thread 0x7f21a001b950 (LWP 5436)]
    0x00007f21a9ba41e5 in *__GI_raise (sig=<value optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
    64 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
    in ../nptl/sysdeps/unix/sysv/linux/raise.c
    Current language: auto; currently c
    (gdb) bt
    #0 0x00007f21a9ba41e5 in *__GI_raise (sig=<value optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
    #1 0x00007f21a9ba5703 in *__GI_abort () at abort.c:88
    #2 0x00007f21ab3e82e5 in qt_message_output (msgType=QtFatalMsg, buf=<value optimized out>) at global/qglobal.cpp:2017
    #3 0x00007f21ab3e8400 in qFatal (msg=<value optimized out>) at global/qglobal.cpp:2216
    #4 0x00007f21ab3eaef9 in QMutex::unlock (this=0x3195cb8) at thread/qmutex.cpp:373
    #5 0x00007f21ab3f158e in QWaitCondition::wait (this=0x3195cb0, mutex=0x3195cb8, time=18446744073709551615) at thread/qwaitcondition_unix.cpp:157
    #6 0x0000000000495f27 in WriteThread::run (this=0x3195c90) at WriteThread.cpp:125
    #7 0x00007f21ab3f0ac9 in QThreadPrivate::start (arg=0x3195c90) at thread/qthread_unix.cpp:189
    #8 0x00007f21aa673007 in start_thread (arg=<value optimized out>) at pthread_create.c:297
    #9 0x00007f21a9c41f4d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
    #10 0x0000000000000000 in ?? ()
    La je voie vraiment plus quoi faire.
    Merci de votre aide.
    Développeur d'Ultracopier

  2. #2
    yan
    yan est déconnecté
    Rédacteur
    Avatar de yan
    Homme Profil pro
    Ingénieur expert
    Inscrit en
    Mars 2004
    Messages
    10 033
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur expert
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2004
    Messages : 10 033
    Points : 13 968
    Points
    13 968
    Par défaut
    ASSERT failure in QMutex::unlock(): "A mutex must be unlocked in the same thread that locked it.", file thread/qmutex.cpp, line 373
    tu utilise des ssignal/slot non?
    Il faut savoir que les slot de QThread ne sont pas exécuté dans la thread qu'il créé.

  3. #3
    Membre régulier
    Avatar de alpha_one_x86
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    411
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Somme (Picardie)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2006
    Messages : 411
    Points : 113
    Points
    113
    Par défaut
    Citation Envoyé par yan Voir le message
    tu utilise des signal/slot non?
    Il faut savoir que les slot de QThread ne sont pas exécuté dans la thread qu'il créé.
    Je sais, et aucun slot n'est dans cette classe. Et la j'ai bien fait gaffe de lock() et unlock() dans le constructeur/destructeur.
    Développeur d'Ultracopier

  4. #4
    yan
    yan est déconnecté
    Rédacteur
    Avatar de yan
    Homme Profil pro
    Ingénieur expert
    Inscrit en
    Mars 2004
    Messages
    10 033
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur expert
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2004
    Messages : 10 033
    Points : 13 968
    Points
    13 968
    Par défaut
    Citation Envoyé par alpha_one_x86 Voir le message
    Je sais, et aucun slot n'est dans cette classe. Et la j'ai bien fait gaffe de lock() et unlock() dans le constructeur/destructeur.
    QThread ne s'exécute pas dans la thread, elle ne fait que la créer.
    N'importe quelle fonction appelé directement est éxécuté dans le thread appelant.

  5. #5
    Membre régulier
    Avatar de alpha_one_x86
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    411
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Somme (Picardie)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2006
    Messages : 411
    Points : 113
    Points
    113
    Par défaut
    Citation Envoyé par yan Voir le message
    QThread ne s'exécute pas dans la thread, elle ne fait que la créer.
    N'importe quelle fonction appelé directement est éxécuté dans le thread appelant.
    Je sais, et seul run() est executé dans le thread par l'appelle de start().
    Développeur d'Ultracopier

  6. #6
    yan
    yan est déconnecté
    Rédacteur
    Avatar de yan
    Homme Profil pro
    Ingénieur expert
    Inscrit en
    Mars 2004
    Messages
    10 033
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur expert
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2004
    Messages : 10 033
    Points : 13 968
    Points
    13 968
    Par défaut
    Citation Envoyé par alpha_one_x86 Voir le message
    Je sais, et seul run() est executé dans le thread par l'appelle de start().
    L'assert est assez explicite.
    Tu as un endroit ou tu lock à partir d'un thread A et tu essaie de le delocker à partir d'un thread B.

  7. #7
    Membre régulier
    Avatar de alpha_one_x86
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    411
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Somme (Picardie)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2006
    Messages : 411
    Points : 113
    Points
    113
    Par défaut
    J'ai trouvé quelque chose de trés intéréssant, quand je remplcae ça:
    if(!theCurrentThread->isRunning())
    theCurrentThread->start();
    par:
    if(theCurrentThread->isFinished())
    theCurrentThread->start();
    J'obtiens un deadlock mais plus de crash. Ce soir je vais regarder plus en détails ce deadlock. J'ai pas le temps la je repart en cour.
    Développeur d'Ultracopier

Discussions similaires

  1. Réponses: 1
    Dernier message: 22/11/2012, 14h20
  2. Crashs : Multithread vs Multiprocess
    Par Dinaïz dans le forum Threads & Processus
    Réponses: 10
    Dernier message: 17/02/2010, 11h46
  3. [Win32]App multithread
    Par billyboy dans le forum Windows
    Réponses: 5
    Dernier message: 25/09/2003, 09h57
  4. Crash Base Access
    Par Ronald G. dans le forum Access
    Réponses: 4
    Dernier message: 04/08/2003, 11h55
  5. Multithreading sous HP Ux 11
    Par pykoon dans le forum Autres éditeurs
    Réponses: 1
    Dernier message: 18/10/2002, 23h36

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo