hi all i have the following multithreaded wxwidgets application and i am testing it , i need the output as the first thread should output the values from 0 to 0xFFFFFFFF and then the second thread and so on..please help
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <wx/thread.h>
#include <wx/log.h>
#include <wx/app.h>
using namespace std;
static wxCriticalSection * critsect = NULL;
class MyThread : public wxThread {
public:
MyThread(unsigned int& c, wxThreadKind kind = wxTHREAD_JOINABLE);
virtual ~MyThread();
protected:
virtual ExitCode Entry();
private:
unsigned int& counter;
};
MyThread::MyThread(unsigned int& c, wxThreadKind kind) : wxThread(kind), counter(c) {
}
MyThread::~MyThread() {
}
wxThread::ExitCode MyThread::Entry() {
for (;;)//make this stuff inside the critical section too
{
critsect->Enter();
if (counter == 0xFFFFFFFF) {
critsect->Leave();
break;
}
++counter;
critsect->Leave();
}
return 0;
//only one thread does operation on critsect
}
int main(int argc, char** argv) {
unsigned int uicounter = 0;
if (argc || argv) argc;
wxInitializer initializer;
if (!initializer) {
fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
return -1;
}
critsect = new wxCriticalSection();
if (critsect == NULL) {
fprintf(stderr, "Failed to create the critical section object, aborting.");
return -2;
}
MyThread *mt = new MyThread(uicounter);
MyThread *mt1 = new MyThread(uicounter);
MyThread *mt2 = new MyThread(uicounter);
MyThread *mt3 = new MyThread(uicounter);
if (mt || mt1 || mt2 || mt3) {
if ((mt->MyThread::Create() == wxTHREAD_NO_ERROR) || (mt1->MyThread::Create() == wxTHREAD_NO_ERROR) || (mt2->MyThread::Create() == wxTHREAD_NO_ERROR) || (mt3->MyThread::Create() == wxTHREAD_NO_ERROR)) {
mt->Run();
mt1->Run();
mt2->Run();
mt3->Run();
if (mt->IsRunning() == true) {
{
cout << uicounter << endl;
}
wxThread::Sleep(1000);
mt1->Delete();
mt2->Delete();
mt3->Delete();
} else if (mt1->IsRunning() == true) {
{
cout << uicounter << endl;
}
wxThread::Sleep(1000);
mt->Delete();
mt2->Delete();
mt3->Delete();
} else if (mt2->IsRunning() == true) {
cout << uicounter << endl;
wxThread::Sleep(1000);
mt1->Delete();
mt->Delete();
mt3->Delete();
} else if (mt3->IsRunning() == true) {
cout << uicounter << endl;
wxThread::Sleep(1000);
mt1->Delete();
mt2->Delete();
mt->Delete();
}
}
}
delete mt;
delete mt1;
delete mt2;
delete mt3;
return 0;
}
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <wx/thread.h>
#include <wx/log.h>
#include <wx/app.h>
using namespace std;
static wxCriticalSection * critsect = NULL;
class MyThread : public wxThread {
public:
MyThread(unsigned int& c, wxThreadKind kind = wxTHREAD_JOINABLE);
virtual ~MyThread();
protected:
virtual ExitCode Entry();
private:
unsigned int& counter;
};
MyThread::MyThread(unsigned int& c, wxThreadKind kind) : wxThread(kind), counter(c) {
}
MyThread::~MyThread() {
}
wxThread::ExitCode MyThread::Entry() {
for (;;)//make this stuff inside the critical section too
{
critsect->Enter();
if (counter == 0xFFFFFFFF) {
critsect->Leave();
break;
}
++counter;
critsect->Leave();
}
return 0;
//only one thread does operation on critsect
}
int main(int argc, char** argv) {
unsigned int uicounter = 0;
if (argc || argv) argc;
wxInitializer initializer;
if (!initializer) {
fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
return -1;
}
critsect = new wxCriticalSection();
if (critsect == NULL) {
fprintf(stderr, "Failed to create the critical section object, aborting.");
return -2;
}
MyThread *mt = new MyThread(uicounter);
MyThread *mt1 = new MyThread(uicounter);
MyThread *mt2 = new MyThread(uicounter);
MyThread *mt3 = new MyThread(uicounter);
if (mt || mt1 || mt2 || mt3) {
if ((mt->MyThread::Create() == wxTHREAD_NO_ERROR) || (mt1->MyThread::Create() == wxTHREAD_NO_ERROR) || (mt2->MyThread::Create() == wxTHREAD_NO_ERROR) || (mt3->MyThread::Create() == wxTHREAD_NO_ERROR)) {
mt->Run();
mt1->Run();
mt2->Run();
mt3->Run();
if (mt->IsRunning() == true) {
{
cout << uicounter << endl;
}
wxThread::Sleep(1000);
mt1->Delete();
mt2->Delete();
mt3->Delete();
} else if (mt1->IsRunning() == true) {
{
cout << uicounter << endl;
}
wxThread::Sleep(1000);
mt->Delete();
mt2->Delete();
mt3->Delete();
} else if (mt2->IsRunning() == true) {
cout << uicounter << endl;
wxThread::Sleep(1000);
mt1->Delete();
mt->Delete();
mt3->Delete();
} else if (mt3->IsRunning() == true) {
cout << uicounter << endl;
wxThread::Sleep(1000);
mt1->Delete();
mt2->Delete();
mt->Delete();
}
}
}
delete mt;
delete mt1;
delete mt2;
delete mt3;
return 0;
}
The Output is :-
13:27:47: Error: Can not resume thread 0 (error 6: the handle is invalid.)
13:27:47: Error: Can not resume thread 0 (error 6: the handle is invalid.)
44359
13:27:47: Error: Can not resume thread 0 (error 6: the handle is invalid.)
13:27:48: Error: Can not resume thread 0 (error 6: the handle is invalid.)
13:27:48: Error: Can not wait for thread termination (error 6: the handle is invalid.)
13:27:48: Error: Couldn't terminate thread (error 6: the handle is invalid.)
13:27:48: Error: Can not resume thread 0 (error 6: the handle is invalid.)
13:27:48: Error: Can not wait for thread termination (error 6: the handle is invalid.)
13:27:48: Error: Couldn't terminate thread (error 6: the handle is invalid.)
13:27:48: Error: Can not resume thread 0 (error 6: the handle is invalid.)
13:27:48: Error: Can not wait for thread termination (error 6: the handle is invalid.)
13:27:48: Error: Couldn't terminate thread (error 6: the handle is invalid.)
RUN SUCCESSFUL (total time: 1s)