#include <QtCore/QCoreApplication>
#include <QThread>
#include <QSemaphore>
#include <QMutex>
#include <stdio.h>
class MyThread:public QThread
{
public:
MyThread( )
{
m_pFile =fopen("./My.txt", "w" );
m_flag =false;
}
~MyThread()
{
fclose(m_pFile );
}
void Fun( )
{
m_flag =true;
for(int i=20000; i<30000; i++ )
{
m_mutex.lock();
fprintf(m_pFile, "%d\n", i);
m_mutex.unlock();
}
m_semaphore.release( );
m_flag =false;
}
protected:
virtual void run ()
{
if (m_flag)
{
m_semaphore.acquire( );
}
for (int i=0; i<20000; i++ )
{
fprintf(m_pFile, "%d\n", i);
}
}
private:
bool m_flag;
QMutex m_mutex;
FILE* m_pFile;
QSemaphore m_semaphore;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread myThread;
myThread.start( );
myThread.Fun();
return a.exec();
}
#include <QThread>
#include <QSemaphore>
#include <QMutex>
#include <stdio.h>
class MyThread:public QThread
{
public:
MyThread( )
{
m_pFile =fopen("./My.txt", "w" );
m_flag =false;
}
~MyThread()
{
fclose(m_pFile );
}
void Fun( )
{
m_flag =true;
for(int i=20000; i<30000; i++ )
{
m_mutex.lock();
fprintf(m_pFile, "%d\n", i);
m_mutex.unlock();
}
m_semaphore.release( );
m_flag =false;
}
protected:
virtual void run ()
{
if (m_flag)
{
m_semaphore.acquire( );
}
for (int i=0; i<20000; i++ )
{
fprintf(m_pFile, "%d\n", i);
}
}
private:
bool m_flag;
QMutex m_mutex;
FILE* m_pFile;
QSemaphore m_semaphore;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread myThread;
myThread.start( );
myThread.Fun();
return a.exec();
}