can not understand this SEGFAULT [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am programming a c/c++ SDL synth which works on linux, windows, and handled psp and I mix c/c++ in multiple part of the code.
I use new/malloc and I don't think I mix them up but if I do please tell me where and why.
My main program is now near 20 000 line and it works fine.
But I allways fall into a bug when I began to modify it and I can not understand and spot my real error.
I fall into SIGSEV when I began to modify some part of my code and it take me hours to make it work without understand why I fall into this.
And after some modification it works again, and I have no clue about why I have a SIGSEV and why it is fixed now and how I can have modify it to make it works and prevent future mistake.
So I ask you to explain it what is my real error.
Here is a gdb log with a really strip down version and the kind of crash I have :
yoyz#yoyz-laptop:~/build/audio/picoloop/tmp/ gdb ./WaveTable
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/yoyz/build/audio/picoloop/tmp/WaveTable...done.
(gdb) r
Starting program: /home/yoyz/build/audio/picoloop/tmp/WaveTable
warning: the debug information found in "/lib64/ld-2.13.so" does not match "/lib64/ld-linux-x86-64.so.2" (CRC mismatch).
Generator::init() Allocating memory
Generator::one() 0x00604010
Generator::sine() 0x00604010
Generator::saw() 0x00604010
Generator::pulse() 0x00604010
Program received signal SIGSEGV, Segmentation fault.
_int_malloc (av=0x7ffff7639e40, bytes=32768) at malloc.c:4738
4738 malloc.c: No such file or directory.
(gdb)
Here is the strip down version of the code :
#include "MyMaster.h"
#include "Generator.h"
#include "WaveTable.h"
#include "WaveTableManager.h"
int main(int argc,char **argv)
{
Generator G;
WaveTableManager & WTM = WaveTableManager::getInstance();
WaveTable* WT;
G.init();
WT = new WaveTable();
WT->setSize(WAVETABLE_SIZE);
G.one();
memcpy(WT->getBuffer(),G.getBuffer(),WAVETABLE_SIZE*DEFAULTBITRATE/8);
WTM.insert(WT,PICO_WAVETABLE_ONE);
WT = new WaveTable();
WT->setSize(WAVETABLE_SIZE);
G.sine();
memcpy(WT->getBuffer(),G.getBuffer(),WAVETABLE_SIZE*DEFAULTBITRATE/8);
WTM.insert(WT,PICO_WAVETABLE_SINE);
WT = new WaveTable();
WT->setSize(WAVETABLE_SIZE);
G.saw();
memcpy(WT->getBuffer(),G.getBuffer(),WAVETABLE_SIZE*DEFAULTBITRATE/8);
WTM.insert(WT,PICO_WAVETABLE_SAW);
WT = new WaveTable();
WT->setSize(WAVETABLE_SIZE);
G.pulse();
memcpy(WT->getBuffer(),G.getBuffer(),WAVETABLE_SIZE*DEFAULTBITRATE/8);
WTM.insert(WT,PICO_WAVETABLE_PULSE);
WT = new WaveTable();
WT->setSize(WAVETABLE_SIZE);
G.triangle();
memcpy(WT->getBuffer(),G.getBuffer(),WAVETABLE_SIZE*DEFAULTBITRATE/8);
WTM.insert(WT,PICO_WAVETABLE_TRGL);
WT = new WaveTable();
WT->setSize(WAVETABLE_SIZE);
G.noise();
memcpy(WT->getBuffer(),G.getBuffer(),WAVETABLE_SIZE*DEFAULTBITRATE/8);
WTM.insert(WT,PICO_WAVETABLE_NOISE);
printf("wavetablemanager.getSize : %d\n",WTM.getSize());
}
MyMaster.h
#ifndef __MASTER____
#define __MASTER____
#include <SDL/SDL.h>
#define WAVETABLE_SIZE 1024*16
#define DEFAULTBITRATE 16
enum
{
PICO_WAVETABLE_SINE,
PICO_WAVETABLE_SAW,
PICO_WAVETABLE_PULSE,
PICO_WAVETABLE_TRGL,
PICO_WAVETABLE_NOISE,
PICO_WAVETABLE_ONE,
PICO_WAVETABLE_SIZE
};
#endif
Generator.h
using namespace std;
#include <SDL/SDL_types.h>
#include <math.h>
#include "MyMaster.h"
#ifndef __GENERATOR__
#define __GENERATOR__
class Generator
{
public:
Generator();
~Generator();
void init();
void sine();
void saw();
void pulse();
void triangle();
void noise();
void one();
Sint16 * getBuffer();
private:
Sint16 * table;
int table_size;
int index;
int d;
};
#endif
Generator.cpp
#include "Generator.h"
Generator::Generator()
{
table_size=WAVETABLE_SIZE;
}
Generator::~Generator()
{
}
void Generator::init()
{
if (table_size>0)
{
printf("Generator::init() Allocating memory\n");
table=(Sint16*)malloc(sizeof(Sint16)*table_size);
if (table==0)
{
printf("Error allocating memory\n");
//return 0;
}
}
}
void Generator::sine()
{
int i;
float f;
Sint16 s;
Sint16 bitdepth=16-1;
printf("Generator::sine() 0x%08.8X\n",table);
for (i=0;i<table_size;i++)
{
s=sin((2*3.14159*i*1)/table_size)*(1<<bitdepth-2);
table[i]=s;
//printf("table[%d]=%d\n",i,s);
}
}
void Generator::saw()
{
int i;
float f;
Sint16 s;
Sint16 bitdepth=16;
Sint16 dec;
printf("Generator::saw() 0x%08.8X\n",table);
s=(1<<(bitdepth-2));
dec=(1<<(bitdepth-2))/(table_size/2);
for (i=0;i<table_size;i++)
{
table[i]=s;
s=s-dec;
}
}
void Generator::pulse()
{
int i;
float f;
Sint16 s;
Sint16 bitdepth=16;
Sint16 dec=(1<<(bitdepth-2))/(table_size/2);
printf("Generator::pulse() 0x%08.8X\n",table);
for (i=0;i<table_size/2;i++)
{
table[i]=((1<<(bitdepth-2))/2);
}
for (i=table_size/2;i<table_size;i++)
{
table[i]=((1<<(bitdepth-2))*-1)/2;
}
}
void Generator::triangle()
{
int i;
float f;
Sint16 s=0;
Sint16 bitdepth=16;
Sint16 dec=(1<<(bitdepth-2))/(table_size/4);
printf("Generator::triangle() 0x%08.8X\n",table);
//table=(Sint16*)malloc(sizeof(Sint16)*table_size);
for (i=0;i<(table_size*1)/4;i++)
{
table[i]=s;
s=s+dec;
}
for (i=(table_size*1)/4;i<(table_size*3)/4;i++)
{
table[i]=s;
s=s-dec;
}
for (i=(table_size*3)/4;i<table_size;i++)
{
table[i]=s;
s=s+dec;
}
}
void Generator::noise()
{
int i;
float f;
Sint16 s;
Sint16 bitdepth=16;
printf("Generator::noise() 0x%08.8X\n",table);
srand(1<<(bitdepth-2));
for (i=0;i<table_size;i++)
{
if (rand()%2==0)
table[i]=rand()%8192;
else
table[i]=(rand()%8192)*-1;
}
}
void Generator::one()
{
int i;
float f;
Sint16 s;
Sint16 bitdepth=16;
printf("Generator::one() 0x%08.8X\n",table);
for (i=0;i<table_size;i++)
{
table[i]=1<<bitdepth-1;
}
}
Sint16 * Generator::getBuffer()
{
return table;
}
WaveTable.h
#include "MyMaster.h"
#include <SDL/SDL_types.h>
#ifndef __WAVETABLE__
#define __WAVETABLE__
class WaveTable
{
public:
WaveTable();
~WaveTable();
int setSize(int bufferSize);
int allocMemory();
int freeMemory();
Sint16 * getBuffer();
char * getName();
Sint32 getSize();
private:
Sint32 size;
Sint16 * buffer;
char * name;
};
#endif
WaveTable.cpp
#include "WaveTable.h"
using namespace std;
WaveTable::WaveTable()
{
size=0;
buffer=0;
name=0;
}
WaveTable::~WaveTable()
{
}
int WaveTable::allocMemory()
{
if (size>0)
{
buffer=(Sint16*)malloc(sizeof(Sint16)*size);
if (buffer==0)
{
printf("Error allocating memory\n");
return 0;
}
}
return size;
}
int WaveTable::freeMemory()
{
if (buffer!=0)
{
free(buffer);
buffer=0;
}
}
int WaveTable::setSize(int bufferSize)
{
if (bufferSize>=0)
size=bufferSize;
if (buffer!=0)
this->freeMemory();
return this->allocMemory();
}
Sint16 * WaveTable::getBuffer()
{
return buffer;
}
WaveTableManager.h
using namespace std;
#include <vector>
#include "WaveTable.h"
#ifndef __WAVETABLEMANAGER__
#define __WAVETABLEMANAGER__
class WaveTableManager
{
private:
WaveTableManager();
~WaveTableManager();
vector<WaveTable*> wtvector;
int size;
public:
static WaveTableManager& getInstance();
int getSize();
void insert(WaveTable * WT,int position);
WaveTable * get(int position);
};
#endif
WaveTableManager.cpp
#include "WaveTableManager.h"
WaveTableManager::WaveTableManager() : wtvector()
{
size=0;
}
WaveTableManager::~WaveTableManager()
{
}
WaveTableManager& WaveTableManager::getInstance()
{
static WaveTableManager instance;
return instance;
}
int WaveTableManager::getSize()
{
return wtvector.size();
}
void WaveTableManager::insert(WaveTable * WT,int position)
{
if (wtvector.size()<=position)
wtvector.resize(position);
wtvector[position]=WT;
}
WaveTable * WaveTableManager::get(int position)
{
return wtvector[position];
}
Makefile.WaveTable
CC=g++
CFLAGS=-O0 -DLINUX -D__RTAUDIO__ -DLINUX_DESKTOP -I. -LSDL/lib -g -fpermissive
SOURCES=WaveTableTest.cpp WaveTable.cpp WaveTableManager.cpp Generator.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=WaveTable
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $#
.cpp.o:
$(CC) -c $(CFLAGS) $< -o $#
clean:
-rm -f $(OBJECTS) $(EXECUTABLE)

Here is one issue:
void WaveTableManager::insert(WaveTable * WT,int position)
{
if (wtvector.size()<=position)
wtvector.resize(position);
wtvector[position]=WT; // < -- Out of bounds access
}
When you call resize(), the upper bound is vector::size()-1. Since position is the new size of the vector, what you probably want is this:
wtvector[position - 1] = WT;

Related

Populate and execute callbacks from array class attribute

I've recently started learning C++/Arduino and am working on abstracting some of my Arduino code in order to keep it more manageable.
I'm trying to construct a class with 2 arrays as attributes on it, one to store strings that represent commands, and a second one to store pointers to those functions.
The below code works (compiles), but when uploaded to the device both the listen and execute functions don't appear to work. I've searched around quite a lot, but can't find where I've gone wrong.
/* main.ino */
// SETUP
#include "SoftwareSerial.h"
SoftwareSerial bt(btRx, btTx);
#include "CMD.h"
const int cmdMax = 6;
ArriCMD cmd;
// COMMANDS
void cmdStatus()
{
Serial.println("OK");
}
// START
void setup()
{
Serial.begin(9600);
bt.begin(9600);
cmd.add("AH+STAT", cmdStatus);
}
void loop()
{
cmd.listen(bt);
}
/* ArriCMD.h */
#ifndef ArriCMD_h
#define ArriCMD_h
#include "Arduino.h"
#include "SoftwareSerial.h"
class ArriCMD
{
public:
ArriCMD();
void add(String cmd, void (*cb)());
void listen(SoftwareSerial serial);
void execute(String cmd);
private:
int _max = 16;
int _amt = 0;
String _cmds[16];
void (*_cbs[16])();
};
/* ArriCMD.cpp */
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "ArriCMD.h"
ArriCMD::ArriCMD()
{
//
}
void ArriCMD::add(String cmd, void (*cb)())
{
if (_amt < _max) {
_cmds[_amt] = cmd;
_cbs[_amt] = *cb;
}
}
void ArriCMD::listen(SoftwareSerial serial)
{
if (serial.available()) {
String cmd = serial.readString();
Serial.print(cmd);
execute(cmd);
}
}
void ArriCMD::execute(String cmd)
{
for (int i = 0; i < _amt; i++) {
if (cmd == _cmds[i]) {
_cbs[i]();
}
}
}
While I've been programming for over a decade, C++ and microcontrollers are brand new to me, any and all help here would be hugely appreciated.
I do intend to open source these libraries, and the subsequent platforms they're built for, once I'm more comfortable with my code quality.
Looks like you forgot to increment your commands counter _amt
void ArriCMD::add(String cmd, void (*cb)())
{
if (_amt < _max) {
_cmds[_amt] = cmd;
_cbs[_amt] = *cb;
_amt++; // <-- here, don't you need it?
}
}
Apart from that, is there some particular reason for using raw array and raw function pointers in your code? I do not use Arduino, so I am not sure, but maybe this solution is a bit cleaner:
class ArriCMD
{
public:
ArriCMD();
void add(String cmd, std::function<void()> cb);
void listen(SoftwareSerial serial);
void execute(String cmd);
private:
std::map<String, std::function<void()> > _cmdMap;
};

Huge ammount of errors during compilation of program with Magick++

During the compilation of a program I was making, that uses the Magick++ library, there appeared an incredibily huge ammount of errors.
This is the command line I used for compile it (g++'s version is 4.9.2, running on Ubuntu 12.04):
g++ -std=c++14 -g -fopenmp `Magick++-config --cppflags --cxxflags` main.cpp -o glitch_img2 `Magick++-config --ldflags --libs`
Here is the program's code:
#include <iostream>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cmath>
#include <string>
#ifdef __OPENMP
#include <omp.h>
#endif
#include <Magick++.h>
double d_max(double a,double b)
{
return (a>b) ? a : b;
}
int ui_clip(int x,int a,int b)
{
return (x<b) ? b : ((x>a) ? a : x);
}
int transform(int x)
{
return ((x*32+1654)%21+x*11+642)%164%33%16;
}
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
{
InitializeMagick(*argv);
Image img1;
img1.read(argv[1]);
int size_x = img1.columns();
int size_y = img1.rows();
int np = size_x*size_y;
Image out;
out.size(Geometry(size_x,size_y));
double *buff_img1 = new double[3*size_x*size_y];
double *buff_out = new double[3*size_x*size_y];
img1.write(0,0,size_x,size_y,"RGB",DoublePixel,buff_img1);
int ix, iy;
#ifdef __OPENMP
#pragma omp for private(ix,iy) schedule(static)
#endif
for (iy=0;iy<size_y;iy++)
{
for (ix=0;ix<size_x;ix++)
{
int loc = (size_x*iy+ix)*3;
double pimg1[3] = {
buff_img1[3*loc ],
buff_img1[3*loc+1],
buff_img1[3*loc+2]
};
int x1 = (ix*(1+transform(ix)))%size_x;
int y1 = (iy*(1+transform(iy)))%size_y;
int loc1 = ui_clip((size_x*y1+x1)%np,0,np-1)*3;
double out_pix[3] = {
buff_img1[3*loc1 ],
buff_img1[3*loc1+1],
buff_img1[3*loc1+2]
};
buff_out[3*loc ] = abs(out_pix[0]-pimg1[0]);
buff_out[3*loc+1] = abs(out_pix[1]-pimg1[1]);
buff_out[3*loc+2] = abs(out_pix[2]-pimg1[2]);
}
}
out.read(size_x,size_y,"RGB",DoublePixel,buff_out);
out.write("Output.png");
}
And those errors are posted here.
What causes such errors and how can they be fixed?
EDIT: The mentioned behaviour dissapears when I remove the Magick++.h header.

why initstate_r crashes when using variables on the stack

Sorry this question not end yet.
I'm trying to write a thread-safe random generator, but
it crashes at
initstate_r(time(NULL), m_state_buff, sizeof(m_state_buff), &m_data);
My gcc version:
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
class RandomGenerator
{
public:
RandomGenerator()
{
initstate_r(time(NULL), m_state_buff, sizeof(m_state_buff), &m_data);
}
virtual ~RandomGenerator() {}
public:
int32_t get_next_int()
{
#ifdef __GNUC__
static __thread char stat_buff[512];
static __thread random_data buff;
static __thread bool inited = false;
if (!inited)
{
initstate_r(time(NULL), stat_buff, sizeof(stat_buff), &buff);
srandom_r(time(NULL), &buff);
inited = true;
}
int32_t result = 0;
random_r(&buff, &result);
return result;
#endif
}
private:
struct random_data m_data;
char m_state_buff[512];
};
int main()
{
RandomGenerator r;
for (int i = 0; i < 100; ++i)
{
printf("%d\n", r.get_next_int());
}
}
I believe this related to the libc initstate_r() 's bug.
An initialization method should not re-use the buffer's value, it should clean it first.
When I cleanup the buffer before call initstate_r() the segmentation fault has gone.
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
class RandomGenerator
{
public:
RandomGenerator()
{
memset(m_state_buff, 0, sizeof(m_state_buff));
memset(&m_data, 0, sizeof(m_data);
initstate_r(time(NULL), m_state_buff, sizeof(m_state_buff), &m_data);
}
virtual ~RandomGenerator() {}
private:
struct random_data m_data;
char m_state_buff[512];
};
int main()
{
RandomGenerator r;
for (int i = 0; i < 100; ++i)
{
printf("%d\n", r.get_next_int());
}
}

Field '__jmpbuf' could not be resolved -cpp

I get this error when trying to compile my program:
Field '__jmpbuf' could not be resolved
I looked for a solution for hours and can't seem to find out where is the culprit.
The Thread.h file contains the header of the class. It has the private member:
sigjmp_buf _env;
And the implementation is inside Thread.cpp:
#include "Thread.h"
#include <setjmp.h>
#include "translateAdd.h"
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#define COUNTER_INIT -1
int Thread::_idCounter = COUNTER_INIT;
Thread::Thread(void (*threadsFunc)(void))
: threadsFunction(threadsFunc), _stack(new char[STACK_SIZE]), _quantums(1)
{
address_t sp, pc;
sp = (address_t)_stack + STACK_SIZE - sizeof(address_t);
pc = (address_t)threadsFunc;
// set environment for later return
sigsetjmp(_env, 1);
(_env->__jmpbuf)[JB_SP] = translate_address(sp);
(_env->__jmpbuf)[JB_PC] = translate_address(pc);
sigemptyset(&_env->__saved_mask);
_id = ++_idCounter;
_state = READY;
}
EDIT: Using eclipse as the IDE under ubuntu 32bit
EDIT: Another complete example that doesn't compile on my machine:
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#define SECOND 1000000
#define STACK_SIZE 4096
char stack1[STACK_SIZE];
char stack2[STACK_SIZE];
sigjmp_buf env[2];
#ifdef __x86_64__
/* code for 64 bit Intel arch */
typedef unsigned long address_t;
#define JB_SP 6
#define JB_PC 7
/* A translation is required when using an address of a variable.
Use this as a black box in your code. */
address_t translate_address(address_t addr)
{
address_t ret;
asm volatile("xor %%fs:0x30,%0\n"
"rol $0x11,%0\n"
: "=g" (ret)
: "0" (addr));
return ret;
}
#else
/* code for 32 bit Intel arch */
typedef unsigned int address_t;
#define JB_SP 4
#define JB_PC 5
/* A translation is required when using an address of a variable.
Use this as a black box in your code. */
address_t translate_address(address_t addr)
{
address_t ret;
asm volatile("xor %%gs:0x18,%0\n"
"rol $0x9,%0\n"
: "=g" (ret)
: "0" (addr));
return ret;
}
#endif
void switchThreads(void)
{
static int currentThread = 0;
int ret_val = sigsetjmp(env[currentThread],1);
printf("SWITCH: ret_val=%d\n", ret_val);
if (ret_val == 1) {
return;
}
currentThread = 1 - currentThread;
siglongjmp(env[currentThread],1);
}
void f(void)
{
int i = 0;
while(1){
++i;
printf("in f (%d)\n",i);
if (i % 3 == 0) {
printf("f: switching\n");
switchThreads();
}
usleep(SECOND);
}
}
void g(void)
{
int i = 0;
while(1){
++i;
printf("in g (%d)\n",i);
if (i % 5 == 0) {
printf("g: switching\n");
switchThreads();
}
usleep(SECOND);
}
}
void setup(void)
{
address_t sp, pc;
sp = (address_t)stack1 + STACK_SIZE - sizeof(address_t);
pc = (address_t)f;
sigsetjmp(env[0], 1);
(env[0]->__jmpbuf)[JB_SP] = translate_address(sp);
(env[0]->__jmpbuf)[JB_PC] = translate_address(pc);
sigemptyset(&env[0]->__saved_mask);
sp = (address_t)stack2 + STACK_SIZE - sizeof(address_t);
pc = (address_t)g;
sigsetjmp(env[1], 1);
(env[1]->__jmpbuf)[JB_SP] = translate_address(sp);
(env[1]->__jmpbuf)[JB_PC] = translate_address(pc);
sigemptyset(&env[1]->__saved_mask);
}
int main(void)
{
setup();
siglongjmp(env[0], 1);
return 0;
}
If you really need to use the internal fields (which will only be valid for your compiler on your system) you need to check the types:
typedef struct __jmp_buf_tag sigjmp_buf[1];
That means that sigjmp_buf is not a pointer, but an array with a single structure in it. So you use it like a normal array of structures:
sigjmp_buf _env;
_env[0].__jmpbuf[x] = y;
I really recommend against the use the internal field of this structure. Linux have other functions to simplify cooperative threading (which is what you seem to be implementing).

Embedding matplotlib in C++

I am reading a message from a socket with C++ code and am trying to plot it interactively with matplotlib, but it seems Python code will block the main thread, no matter I use show() or ion() and draw(). ion() and draw() won't block in Python.
Any idea how to plot interactively with matplotlib in C++ code?
An example would be really good.
Thanks a lot.
You may also try creating a new thread that does the call to the
blocking function, so that it does not block IO in your main program
loop. Use an array of thread objects and loop through to find an unused
one, create a thread to do the blocking calls, and have another thread
that joins them when they are completed.
This code is a quick slap-together I did to demonstrate what I mean about
using threads to get pseudo asynchronous behavior for blocking functions...
I have not compiled it or combed over it very well, it is simply to show
you how to accomplish this.
#include <pthread.h>
#include <sys/types.h>
#include <string>
#include <memory.h>
#include <malloc.h>
#define MAX_THREADS 256 // Make this as low as possible!
using namespace std;
pthread_t PTHREAD_NULL;
typedef string someTypeOrStruct;
class MyClass
{
typedef struct
{
int id;
MyClass *obj;
someTypeOrStruct input;
} thread_data;
void draw(); //Undefined in this example
bool getInput(someTypeOrStruct *); //Undefined in this example
int AsyncDraw(MyClass * obj, someTypeOrStruct &input);
static void * Joiner(MyClass * obj);
static void * DoDraw(thread_data *arg);
pthread_t thread[MAX_THREADS], JoinThread;
bool threadRunning[MAX_THREADS], StopJoinThread;
bool exitRequested;
public:
void Main();
};
bool MyClass::getInput(someTypeOrStruct *input)
{
}
void MyClass::Main()
{
exitRequested = false;
pthread_create( &JoinThread, NULL, (void *(*)(void *))MyClass::Joiner, this);
while(!exitRequested)
{
someTypeOrStruct tmpinput;
if(getInput(&tmpinput))
AsyncDraw(this, tmpinput);
}
if(JoinThread != PTHREAD_NULL)
{
StopJoinThread = true;
pthread_join(JoinThread, NULL);
}
}
void *MyClass::DoDraw(thread_data *arg)
{
if(arg == NULL) return NULL;
thread_data *data = (thread_data *) arg;
data->obj->threadRunning[data->id] = true;
// -> Do your draw here <- //
free(arg);
data->obj->threadRunning[data->id] = false; // Let the joinThread know we are done with this handle...
}
int MyClass::AsyncDraw(MyClass *obj, someTypeOrStruct &input)
{
int timeout = 10; // Adjust higher to make it try harder...
while(timeout)
{
for(int i = 0; i < MAX_THREADS; i++)
{
if(thread[i] == PTHREAD_NULL)
{
thread_data *data = (thread_data *)malloc(sizeof(thread_data));
if(data)
{
data->id = i;
data->obj = this;
data->input = input;
pthread_create( &(thread[i]), NULL,(void* (*)(void*))MyClass::DoDraw, (void *)&data);
return 1;
}
return 0;
}
}
timeout--;
}
}
void *MyClass::Joiner(MyClass * obj)
{
obj->StopJoinThread = false;
while(!obj->StopJoinThread)
{
for(int i = 0; i < MAX_THREADS; i++)
if(!obj->threadRunning[i] && obj->thread[i] != PTHREAD_NULL)
{
pthread_join(obj->thread[i], NULL);
obj->thread[i] = PTHREAD_NULL;
}
}
}
int main(int argc, char **argv)
{
MyClass base;
base.Main();
return 0;
}
This way you can continue accepting input while the draw is occurring.
~~Fixed so the above code actually compiles, make sure to add -lpthread