PDA

View Full Version : سوال: quicksort+pthread



sahel1364
جمعه 05 تیر 1388, 20:07 عصر
با سلام
کسی میتونه به من کمک کنه تا quick sortرو با pthreadتو لینوکس بنویسم؟
ببخشید چون جایی پیدا نکردم سوالم رو این جا گذاشتم.

shahryary
شنبه 06 تیر 1388, 14:12 عصر
#ifndef IOSTREAM
#include <iostream>
#define IOSTREAM
#endif

#ifndef LIST
#include <list>
#define LIST
#endif

#ifndef PTHREAD
#include <pthread.h>
#define PTHREAD
#endif

#ifndef STD
using namespace std;
#define STD
#endif

class Thread
{
public:
Thread ();
virtual ~Thread();

bool Start ();
bool Stop ();
bool Running ();
bool WaitOn ();

pthread_t Id ();

static void WaitOnAll ();

protected:
virtual bool Setup ();
virtual bool Run () = 0;



private:
static void* Entry (void* in);
bool running;
pthread_t info;


static list<Thread*> threads;
};

list<Thread*> Thread::threads;

Thread::Thread (): running(false)
{

}

Thread::~Thread()
{
if (running)
{
cout << "killing thread" << endl;
Stop();
this->WaitOn();
}
}

bool Thread::Running ()
{
return running;
}

bool Thread::WaitOn ()
{
if (running)
{
return !(bool)pthread_join(info, NULL);
}
return false;
}

bool Thread::Setup ()
{
return true;
}

bool Thread::Start ()
{
if (!running)
{
running = !(bool)pthread_create(&info, NULL, Entry, (void*)this);
if (running)
Thread::threads.push_back(this);
}
return running;
}

bool Thread::Stop ()
{
if (running)
{
cout << "stopping thread" << endl;
pthread_cancel(info);
running = false;
for (list<Thread*>::iterator l = threads.begin(); l != threads.end(); l++)
if ((*l)->Id() == info)
{
cout << "removing thread: " << *l << endl;
threads.erase(l);
break;
cout << "done removing thread" << endl;
}
return true;
}
return false;
}

pthread_t Thread::Id ()
{
return info;
}

void Thread::WaitOnAll ()
{
while (threads.size())
(*(threads.begin()))->WaitOn();
}

void* Thread::Entry (void* in)
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
Thread* temp = (Thread*)in;
if (temp->Setup())
temp->Run();
return 0;
}

shahryary
شنبه 06 تیر 1388, 14:13 عصر
#ifdef PTHREAD
# include <pthread.h>
/*
* thread and mutex types
*/
typedef pthread_t Thread;
typedef pthread_mutex_t ThreadMutex;
#elif defined (WIN32)
typedef unsigned long Thread;
typedef CRITICAL_SECTION ThreadMutex;
#endif


/* one time initialization of a 'fast' mutex (not reversible) */
void initMutex(void* pMutex)
{
#if defined (PTHREAD)
pthread_mutex_init((pthread_mutex_t*

)pMutex, pthread_mutexattr_default);
#elif defined (WIN32)
InitializeCriticalSection((PCRITICAL_SECTION)pMute x);
#endif
}

/* enter critical section and block others threads or wait */
void enterMutex(void* pMutex)
{
#if defined (PTHREAD)
pthread_mutex_lock((pthread_mutex_t*)pMutex);
#elif defined (WIN32)
EnterCriticalSection((PCRITICAL_SECTION)pMutex);
#endif
}

/* leave critical section and release any other thread blocked */
void leaveMutex(void* pMutex)
{
#if defined (PTHREAD)
pthread_mutex_unlock((pthread_mutex_t*)pMutex);
#elif defined (WIN32)
LeaveCriticalSection((PCRITICAL_SECTION)pMutex);
#endif
}


You would use this like:


ThreadMutex mutex; // create global mutex

int main()
{
initMutex(&mutex);

// start some threads ...

while (true)
{
doSomething();
if (needToWriteToLogFile())
{
// either we get the resource (logfile) exclusively or we have to wait
enterMutex(&mutex);

ofstream oflog("logfile.log", ios::append | ios::out);
writeLog(oflog, "Any message");
oflog.close();

leaveMutex(&mutex);
}
}
}


// thread.cpp

void threadFunction(void* pParam)
{
while (true)
{
doSomeThreadThings();
if (needToWriteToLogFile())
{
// either we get the resource (logfile) exclusively or we have to wait
enterMutex(&mutex);

ofstream oflog("logfile.log", ios::append | ios::out);
writeLog(oflog, "Any message");
oflog.close();

leaveMutex(&mutex);
}
}
}