Threads in QNX4 and in QNX6

Hi Community!!!

Two questions first QNX4

if I use this QNX4 code

#define STACK_SIZE 1024
#include <process.h>
#include <stdlib.h>

int i, counter = 0;

void thread2(void*);

int main(void)
{
pid_t tid;
tid = _beginthread(thread2, NULL, STACK_SIZE, NULL);

for (i = 0; i < 11; i ++)
printf(“print thread1 %i\n”, counter ++);

return (EXIT_SUCCESS);
}

void thread2(void *parm)
{
for (i = 0; i < 11; i ++)
printf(“print thread2 %i\n”, counter);

_endthread();
}

The result in shell is:

print thread2 0
print thread1 0
print thread2 1
print thread1 1
print thread2 2
print thread1 2
print thread2 3
print thread1 3
print thread2 4
print thread1 4
print thread2 5
print thread1 5

Why output is not until 10 like this?

print thread2 0
print thread1 0
print thread2 1
print thread1 1
print thread2 2
print thread1 2
print thread2 3
print thread1 3
print thread2 4
print thread1 4
print thread2 5
print thread1 5
print thread2 6
print thread1 6
print thread2 7
print thread1 7
print thread2 8
print thread1 8
print thread2 9
print thread1 9
print thread2 10
print thread1 10

and the second question in QNX6

if I use this QNX6 code:


#include <stdlib.h>
int zaehler = 0;

void *thread2(void *not_used)
{
int i;

for (i = 0; i < 11; i ++)
{
printf(“print thread2 %i\n”, zaehler);
}

return (EXIT_SUCCESS);
}

int main(void)
{
int i;
pthread_create(NULL, NULL, thread2, NULL);

for (i = 0; i < 11; i ++)
{
printf(“print thread1 %i\n”, zaehler ++);
}

sleep(20); /* Threads auf 20 Sekunden begrenzt */

return (EXIT_SUCCESS);
}

output in shell is:

print thread2 0
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 0
print thread2 1
print thread2 2
print thread2 3
print thread2 4
print thread2 5
print thread2 6
print thread2 7
print thread2 8
print thread2 9
print thread2 10

why output is because of “stdio mutex” not like this:

print thread2 0
print thread2 0
print thread2 0
print thread2 0
print thread2 0
print thread2 0
print thread2 0
print thread2 0
print thread2 0
print thread2 0
print thread2 0
print thread1 0
print thread1 1
print thread1 2
print thread1 3
print thread1 4
print thread1 5
print thread1 6
print thread1 7
print thread1 8
print thread1 9
print thread1 10

does the “stdio mutex” not lock the critical ressource counter?

Thank you

There is no stdio mutex in QNX4. Support for thread in QNX4 is very limited at the OS and C library level.

Thank you mario !!!

Ok but why QNX are counting only until 5 and not until 10?

and why QNX6 Thread2 are changing from 0 to 1? If an mutex is used
resource should be not change please loock to my samples.

Thank You!!!

Because your code is broken, the for loop of each thread uses the same global i variable…

I don’t understand how you can get only thread2 printing data and not thread1. Nor do I understand how it can go back to 0. I think the code and the output you post don’t match.

Yes you right!

output is:

print thread2 0
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread2 1
print thread1 0
print thread1 1
print thread1 2
print thread1 3
print thread1 4
print thread1 5
print thread1 6
print thread1 7
print thread1 8
print thread1 9
print thread1 10

and why thread2 don’t persist to 0 and inkrements to 1 if he is
locked by stdio mutex?

Sorry my mistake !!!

It’s quite difficult to figure out where thread switching occurs, if you have the PE version you can figure it out via the trace facility. That being said the output does make sense to me ;-)