Help for Multi Thread Programming

Dear Sir,
We are from BNF Technology Inc, Taejon, South Korea.
We are the QNX 4.0 license holder and using QNX OS as a server in our
project. This project is fully realtime based. We have extensively used many
features of QNX. But, we stuck at “Thread programming”.
The exact situation is :
(Please, refer the following sample code)
In our program there are two functions which should be executed
continuously, so we implemented these functins in threads. When we compiled
our program, it was compiled successufuly but when executed, it was giving
error for thread 1 “Stack overflow”. Then we reduced the size of stack,
again compiled and executed. then it was giving diferent error “Segmentation
error”. We thought this may be because to less Stack size. So we tried with
all the possibilities of the stack size (increasing and decreasing), but
still we are getting the same errors altenatively. We are not able to decide
the stack size. The used functions are very big. We tried to optimize these
functions also.
So our query is whether, there is any procedure or function which can decide
the stack size automatically for each function in the runtime. In our
program the stack size is constant which is needed to pass in the
“_beginthread” function. Or is there any alternative method to implement
threads? Please, suggest us.

// Sample program
// Reference : From the Help of QNX OS
// Search Topic : _beginthread
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <malloc.h>
#include <process.h>

#define FAR
#define STACK_SIZE 1024 // Stack size for (_386) 32 bit compiler
char *stack1, *stack2;
int ThreadId1;
int ThreadId2;

static volatile int WaitForThread;

// Functions
void Function1()
{
// This is just a sample and we are not passing anything to this
function.
.
.
.
.
.
.
}

void Functin2()
{
// This is just a sample and we are not passing anything to this
function.
.
.
.
.
.
}

// Thread 1
void FAR Thread1( void FAR *parm )
{
printf( “Thread1 ID = %x\n”, *_threadid );
while(1)
{
Function1();
printf( “Inside Thread1 ---------------\n”);
sleep(1);
}
WaitForThread = 0;
_endthread();
}

//Thread 2
void FAR Thread2(void FAR *parm)
{
printf( “Thread2 ID = %x\n”, *_threadid );
while(1)
{
Function2();
printf( “--------------- Inside Thread2\n”);
sleep(1);

}
WaitForThread = 0;
_endthread();
}


int main( void )
{
char *Temp = NULL;

// Allocate the memory
stack1 = (char *) malloc( STACK_SIZE );
stack2 = (char *) malloc( STACK_SIZE );

WaitForThread = 1;

printf(“Before Initialization of Threads\n”);

// Begin both threads
ThreadId1 = _beginthread( Thread1, stack1, STACK_SIZE, Temp);

ThreadId2 = _beginthread( Thread2, stack2, STACK_SIZE, Temp);

printf(“After Initialization of Threads\n”);

// Continue executing
while( WaitForThread ) sleep( 1 );

return( EXIT_SUCCESS );
}


Thank you.


Sunil J. Warke
BNF Technology, Inc.,
150, Duckjin-Dong, Yusong-Ku,
Taejon, South Korea.
Zip: 305 353
Tel: (82)(42) 868 2817
Fax: (82)(42) 862 8828
Mob: 011 9696 2288
Website: http://www.bnftech.com
Email: sunil@bnftech.com

Threads in QNX4 are not really supported. What you use is not reliable.
That’s where the crash is probably coming from.

You have two options:

  • switch to QNX6, which fully supports threads
  • use processes instead of threads

Markus


“Sunil Warke” <sunil@bnftech.com> wrote in message
news:9joe3g$9d3$1@inn.qnx.com…

Dear Sir,
We are from BNF Technology Inc, Taejon, South Korea.
We are the QNX 4.0 license holder and using QNX OS as a server in our
project. This project is fully realtime based. We have extensively used
many
features of QNX. But, we stuck at “Thread programming”.
The exact situation is :
(Please, refer the following sample code)
In our program there are two functions which should be executed
continuously, so we implemented these functins in threads. When we
compiled
our program, it was compiled successufuly but when executed, it was giving
error for thread 1 “Stack overflow”. Then we reduced the size of stack,
again compiled and executed. then it was giving diferent error
“Segmentation
error”. We thought this may be because to less Stack size. So we tried
with
all the possibilities of the stack size (increasing and decreasing), but
still we are getting the same errors altenatively. We are not able to
decide
the stack size. The used functions are very big. We tried to optimize
these
functions also.
So our query is whether, there is any procedure or function which can
decide
the stack size automatically for each function in the runtime. In our
program the stack size is constant which is needed to pass in the
“_beginthread” function. Or is there any alternative method to implement
threads? Please, suggest us.

// Sample program
// Reference : From the Help of QNX OS
// Search Topic : _beginthread
#include <stdio.h
#include <stdlib.h
#include <unistd.h
#include <stddef.h
#include <malloc.h
#include <process.h

#define FAR
#define STACK_SIZE 1024 // Stack size for (_386) 32 bit compiler
char *stack1, *stack2;
int ThreadId1;
int ThreadId2;

static volatile int WaitForThread;

// Functions
void Function1()
{
// This is just a sample and we are not passing anything to this
function.
.
.
.
.
.
.
}

void Functin2()
{
// This is just a sample and we are not passing anything to this
function.
.
.
.
.
.
}

// Thread 1
void FAR Thread1( void FAR *parm )
{
printf( “Thread1 ID = %x\n”, *_threadid );
while(1)
{
Function1();
printf( “Inside Thread1 ---------------\n”);
sleep(1);
}
WaitForThread = 0;
_endthread();
}

//Thread 2
void FAR Thread2(void FAR *parm)
{
printf( “Thread2 ID = %x\n”, *_threadid );
while(1)
{
Function2();
printf( “--------------- Inside Thread2\n”);
sleep(1);

}
WaitForThread = 0;
_endthread();
}


int main( void )
{
char *Temp = NULL;

// Allocate the memory
stack1 = (char *) malloc( STACK_SIZE );
stack2 = (char *) malloc( STACK_SIZE );

WaitForThread = 1;

printf(“Before Initialization of Threads\n”);

// Begin both threads
ThreadId1 = _beginthread( Thread1, stack1, STACK_SIZE, Temp);

ThreadId2 = _beginthread( Thread2, stack2, STACK_SIZE, Temp);

printf(“After Initialization of Threads\n”);

// Continue executing
while( WaitForThread ) sleep( 1 );

return( EXIT_SUCCESS );
}


Thank you.


Sunil J. Warke
BNF Technology, Inc.,
150, Duckjin-Dong, Yusong-Ku,
Taejon, South Korea.
Zip: 305 353
Tel: (82)(42) 868 2817
Fax: (82)(42) 862 8828
Mob: 011 9696 2288
Website: > http://www.bnftech.com
Email: > sunil@bnftech.com

“Sunil Warke” <sunil@bnftech.com> wrote in message
news:9joe3g$9d3$1@inn.qnx.com…

Dear Sir,
We are from BNF Technology Inc, Taejon, South Korea.
We are the QNX 4.0 license holder and using QNX OS as a server in our
project. This project is fully realtime based. We have extensively used
many
features of QNX. But, we stuck at “Thread programming”.
The exact situation is :
(Please, refer the following sample code)
In our program there are two functions which should be executed
continuously, so we implemented these functins in threads. When we
compiled
our program, it was compiled successufuly but when executed, it was giving
error for thread 1 “Stack overflow”. Then we reduced the size of stack,
again compiled and executed. then it was giving diferent error
“Segmentation
error”. We thought this may be because to less Stack size. So we tried
with
all the possibilities of the stack size (increasing and decreasing), but
still we are getting the same errors altenatively. We are not able to
decide
the stack size. The used functions are very big. We tried to optimize
these
functions also.

So our query is whether, there is any procedure or function which can
decide
the stack size automatically for each function in the runtime. In our
program the stack size is constant which is needed to pass in the
“_beginthread” function. Or is there any alternative method to implement
threads? Please, suggest us.

Threads under QNX4 requires lots of attention. QNX4 doesn’t fully support
thread, it can be done with carefull planing. Not all functions
are thread safe. You are using sleep() which is NOT thread safe.
You should also compile with -b. Stack size of 1k is probably too small
I’d start with
8k. When you see the error Stack Overflow that means the stack
isn’t big enough, size must be increased not reduced. There is no way to
set stack size automaticaly.

Are you sure *_threadid is not pointing to NULL?


// Sample program
// Reference : From the Help of QNX OS
// Search Topic : _beginthread
#include <stdio.h
#include <stdlib.h
#include <unistd.h
#include <stddef.h
#include <malloc.h
#include <process.h

#define FAR
#define STACK_SIZE 1024 // Stack size for (_386) 32 bit compiler
char *stack1, *stack2;
int ThreadId1;
int ThreadId2;

static volatile int WaitForThread;

// Functions
void Function1()
{
// This is just a sample and we are not passing anything to this
function.
.
.
.
.
.
.
}

void Functin2()
{
// This is just a sample and we are not passing anything to this
function.
.
.
.
.
.
}

// Thread 1
void FAR Thread1( void FAR *parm )
{
printf( “Thread1 ID = %x\n”, *_threadid );
while(1)
{
Function1();
printf( “Inside Thread1 ---------------\n”);
sleep(1);
}
WaitForThread = 0;
_endthread();
}

//Thread 2
void FAR Thread2(void FAR *parm)
{
printf( “Thread2 ID = %x\n”, *_threadid );
while(1)
{
Function2();
printf( “--------------- Inside Thread2\n”);
sleep(1);

}
WaitForThread = 0;
_endthread();
}


int main( void )
{
char *Temp = NULL;

// Allocate the memory
stack1 = (char *) malloc( STACK_SIZE );
stack2 = (char *) malloc( STACK_SIZE );

WaitForThread = 1;

printf(“Before Initialization of Threads\n”);

// Begin both threads
ThreadId1 = _beginthread( Thread1, stack1, STACK_SIZE, Temp);

ThreadId2 = _beginthread( Thread2, stack2, STACK_SIZE, Temp);

printf(“After Initialization of Threads\n”);

// Continue executing
while( WaitForThread ) sleep( 1 );

return( EXIT_SUCCESS );
}


Thank you.


Sunil J. Warke
BNF Technology, Inc.,
150, Duckjin-Dong, Yusong-Ku,
Taejon, South Korea.
Zip: 305 353
Tel: (82)(42) 868 2817
Fax: (82)(42) 862 8828
Mob: 011 9696 2288
Website: > http://www.bnftech.com
Email: > sunil@bnftech.com

Rather than threading you could fork() your application. However, you loose
the access to global variables so you would need to allocate shared memory
for common data access. As has been mentionned here, threads are only
somewhat supported on QNX4 and many of the functions, particularly Photon
functions, are not thread-safe. I suggest avoiding the use of threads under
this version of the OS.


David,

Sunil Warke <sunil@bnftech.com> wrote in message
news:9joe3g$9d3$1@inn.qnx.com…

Dear Sir,
We are from BNF Technology Inc, Taejon, South Korea.
We are the QNX 4.0 license holder and using QNX OS as a server in our
project. This project is fully realtime based. We have extensively used
many
features of QNX. But, we stuck at “Thread programming”.

“Mario Charest” <mcharest@zinformatic.com> wrote in message
news:9jp4rl$nl0$1@inn.qnx.com…

“Sunil Warke” <> sunil@bnftech.com> > wrote in message
news:9joe3g$9d3$> 1@inn.qnx.com> …
You should also compile with -b.

Woops that’s -bm