getting this error in pthread_create code? - c++

I am creating my Operating system project in which this is my code,
I am using a Linux operating system and I when I am compiling my code, the pthread_create() function is showing an error. The error is related to void return type.
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#
define LEFT(PhilNum + 4) % 5# define RIGHT(PhilNum + 1) % 5
enum s {
EATING,
HUNGRY,
THINKING
};
struct Philosopher {
char * name;
int id;
};
struct Philosopher P[5] = {
{
"Professor A",
0
},
{
"Professor B",
1
},
{
"Professor C",
2
},
{
"Professor D",
3
},
{
"Professor E",
4
}
};
int ProcessCurrantState[5];
sem_t MUTEX;
sem_t Chop[5];
void CheckAvailability(int PhilNum) {
if (ProcessCurrantState[LEFT] != EATING && ProcessCurrantState[RIGHT] != EATING) {
// ProcessCurrantState that eating
ProcessCurrantState[PhilNum] = EATING;
sleep(2);
printf("--------------------------------------------------------\n");
printf("| Philosopher %s ,id : %d | Picking up Chopsticks %d and %d .| \n", P[PhilNum].name,
PhilNum + 1, LEFT + 1, PhilNum + 1);
printf("--------------------------------------------------------\n");
printf("--------------------------------------------------------\n");
printf("Philosopher %s , id : %d | is Eating .\n", P[PhilNum].name, PhilNum + 1);
printf("--------------------------------------------------------\n");
sem_post( & Chop[PhilNum]);
}
}
// take up chopsticks
void PickUpChopSticks(int PhilNum) {
sem_wait( & MUTEX);
// ProcessCurrantState that hungry
ProcessCurrantState[PhilNum] = HUNGRY;
printf("--------------------------------------------------------\n");
printf(" | Philosopher %s , id : %d | is Hungry . |\n", P[PhilNum].name, PhilNum + 1);
printf("--------------------------------------------------------\n");
// eat if neighbours are not eating
CheckAvailability(PhilNum);
sem_post( & MUTEX);
// if unable to eat wait to be signalled
sem_wait( & Chop[PhilNum]);
sleep(1);
}
// put down chopsticks
void PutChopsticksDown(int PhilNum) {
sem_wait( & MUTEX);
// ProcessCurrantState that thinking
ProcessCurrantState[PhilNum] = THINKING;
printf("---------------------------------------------------------\n");
printf("| Philosopher %s , id : %d | puting down Chopsticks %d and %d .|\n", P[PhilNum].name,
PhilNum + 1, LEFT + 1, PhilNum + 1);
printf("--------------------------------------------------------\n");
printf("| Philosopher %s , id : %d | is thinking . | \n", P[PhilNum].name, PhilNum + 1);
printf("--------------------------------------------------------\n");
CheckAvailability(LEFT);
CheckAvailability(RIGHT);
sem_post( & MUTEX);
}
void * philospher(void * num) {
while (1) {
int i = (int * ) num;
sleep(1);
PickUpChopSticks(i);
sleep(0);
PutChopsticksDown(i);
}
}
int main() {
int i;
pthread_t Thread[5];
// initialize the semaphores
sem_init( & MUTEX, 0, 1);
for (i = 0; i < 5; i++) {
sem_init( & Chop[i], 0, 0);
}
// create philosopher processes
for (i = 0; i < 5; i++) {
pthread_create( & Thread[i], NULL, philospher, (void * ) P[i].id);
printf("--------------------------------------------------------\n");
printf("| Philosopher %s , id : %d | is thinking\n . |", P[i].name, i + 1);
printf("--------------------------------------------------------\n");
}
for (i = 0; i < 5; i++)
pthread_join(Thread[i], NULL);
}
and I am getting this error while running
main.cpp: In function ‘void* philospher(void*)’:
main.cpp:97:21: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
int i = (int *)num;

In pthread_create it casts int to void*. The reverse conversion you need is void* to int:
int i = (int)num;
In C++ code you may like to use std::thread instead of pthread_create, so that you can pass thread arguments without casting. E.g.:
void philospher(int num);
// ...
std::thread Thread[5];
// ...
Thread[i] = std::thread(philospher, P[i].id);
//
for(auto& t : Thread)
t.join();

Related

Can not pass values inside a struct, when use the struct pointer as an argument to a thread function correctly?

I wrote a very simply code to use C++11 thread. I found that if I use struct pointer as an argument for the threaded function, the value inside the struct can not be passed correctly.
Please tell me where I did wrong. Thanks.
#include<iostream>
#include<thread>
#include<mutex>
using namespace std;
typedef struct {
int tid;
int bbb;
} arguments;
void blahblah (void * args) {
int tid1 = ((arguments*)args)->tid;
int b = ((arguments*)args)->bbb;
printf("the tid is %d, %d\n", tid1, b);
}
int main () {
int n = 5;
// thread * ThreadArray = (thread*)malloc(n * sizeof(thread));
thread ThreadArray[n];
int tid = 0, bbb = 6;
for (int i = 0; i < n; i++) {
arguments args = {tid, bbb};
tid++;
bbb--;
printf("the data inside %d, %d\n", args.tid, args.bbb);
ThreadArray[i] = thread (blahblah, &args);
}
for (int i = 0; i < n; i++) {
ThreadArray[i].join ();
}
return 1;
}
The result is like:
the data inside 0, 6
the data inside 1, 5
the data inside 2, 4
the data inside 3, 3
the tid is 3, 3
the tid is 3, 3
the tid is 4, 2
the data inside 4, 2
the tid is 4, 2
the tid is 4, 2
You're passing the address of a temporary arguments instance, so you have no guarantees it's valid when your thread tries to use it. Instead, pass the args directly, since C++'s thread library supports arguments by value.
void blahblah (arguments args)
{
printf("the tid is %d, %d\n", args.tid, args.bbb);
}
// ...
for (int i = 0; i < n; i++)
{
arguments args = {tid, bbb};
tid++;
bbb--;
printf("the data inside %d, %d\n", args.tid, args.bbb);
ThreadArray[i] = thread (blahblah, args);
}
Inside of your loop, you are passing a pointer to a local arguments variable that goes out of scope right after the std::thread constructor exits, so the memory is likely to be invalid by the time blahblah() tries to access it.
You would have to declare the arguments memory outside of the loop so it stays around long enough for blahblah() to use it, eg:
#include <thread>
#include <cstdio>
using namespace std;
struct arguments {
int tid;
int bbb;
};
void blahblah (arguments * args) {
int tid1 = args->tid;
int b = args->bbb;
printf("the tid is %d, %d\n", tid1, b);
}
int main() {
const int n = 5;
thread ThreadArray[n];
arguments args[n];
int tid = 0, bbb = 6;
for (int i = 0; i < n; i++) {
args[i].tid = tid++;
args[i].bbb = bbb--;
printf("the data inside %d, %d\n", args[i].tid, args[i].bbb);
ThreadArray[i] = thread(blahblah, &args[i]);
}
for (auto &t : ThreadArray) {
t.join();
}
return 1;
}
Alternatively:
#include <thread>
#include <memory>
#include <cstdio>
using namespace std;
struct arguments {
int tid;
int bbb;
};
void blahblah (unique_ptr<arguments> &args) {
int tid1 = args->tid;
int b = args->bbb;
printf("the tid is %d, %d\n", tid1, b);
}
int main() {
const int n = 5;
thread ThreadArray[n];
int tid = 0, bbb = 6;
for (int i = 0; i < n; i++) {
auto args = make_unique<arguments>();
args->tid = tid++;
args->bbb = bbb--;
printf("the data inside %d, %d\n", args->tid, args->bbb);
ThreadArray[i] = thread(blahblah, move(args));
}
for (auto &t : ThreadArray) {
t.join();
}
return 1;
}
Or, you can pass the arguments by value instead:
#include <thread>
#include <cstdio>
using namespace std;
struct arguments {
int tid;
int bbb;
};
void blahblah (arguments args) {
int tid1 = args.tid;
int b = args.bbb;
printf("the tid is %d, %d\n", tid1, b);
}
int main() {
const int n = 5;
thread ThreadArray[n];
int tid = 0, bbb = 6;
for (int i = 0; i < n; i++) {
arguments args{tid++, bbb--};
printf("the data inside %d, %d\n", args.tid, args.bbb);
ThreadArray[i] = thread(blahblah, args);
}
for (auto &t : ThreadArray) {
t.join();
}
return 1;
}
Or, you caan simply get rid of arguments altogether, as std::thread supports functions with multiple parameters:
#include <thread>
#include <cstdio>
using namespace std;
void blahblah (int tid, int bbb) {
printf("the tid is %d, %d\n", tid, bbb);
}
int main() {
const int n = 5;
thread ThreadArray[n];
int tid = 0, bbb = 6;
for (int i = 0; i < n; i++) {
printf("the data inside %d, %d\n", tid, bbb);
ThreadArray[i] = thread(blahblah, tid++, bbb--);
}
for (auto &t : ThreadArray) {
t.join();
}
return 1;
}

C/C++ threads magic difference in condition

I wanted to write simple multithread app in C/C++. Function funProducent produces 100 values and if random generated value is in given range, char is added to buffer. Function funKonzument comsumes values from buffer. Here is my code:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#define BUFFER_LIMIT 20
struct struktura{
pthread_mutex_t mutex;
pthread_cond_t bufferNotFull;
pthread_cond_t bufferNotEmpty;
int bufferIndex;
char * buffer;
int junk;
};
void * funProducent(void *arg){
struktura * data = (struktura *) arg;
int i = 0;
while (i < 100) {
pthread_mutex_lock(&data->mutex);
if(data->bufferIndex == BUFFER_LIMIT - 1){
pthread_cond_wait(&data->bufferNotFull, &data->mutex);
}
int randomValue = (rand() % 20) + 1;
if( randomValue < 13 ){
data->buffer[++data->bufferIndex] = 'a';
printf("%2d : Producent at index %d added %c\n", i, data->bufferIndex, data->buffer[data->bufferIndex]);
pthread_cond_signal(&data->bufferNotEmpty);
} else {
data->junk++;
}
pthread_mutex_unlock(&data->mutex);
i++;
}
printf("producent is done\n");
}
void * funKonzument(void *arg){
struktura * data = (struktura *) arg;
int i = 0;
while (i + data->junk < 100) {
printf("%d\n", i + data->junk);
pthread_mutex_lock(&data->mutex);
if(data->bufferIndex < 0){
pthread_cond_wait(&data->bufferNotEmpty, &data->mutex);
}
printf("%2d : Konzument at index %d consumed %c\n", i, data->bufferIndex, data->buffer[data->bufferIndex]);
data->bufferIndex--;
pthread_cond_signal(&data->bufferNotFull);
pthread_mutex_unlock(&data->mutex);
i++;
}
printf("konzument is done\n");
}
int main(int argc, char** argv) {
pthread_t threadProducent, threadKonzument;
struktura threadData;
threadData.buffer = (char *) malloc(sizeof(char) * BUFFER_LIMIT);
threadData.bufferIndex = -1;
threadData.bufferNotFull = PTHREAD_COND_INITIALIZER;
threadData.bufferNotEmpty = PTHREAD_COND_INITIALIZER;
threadData.mutex = PTHREAD_MUTEX_INITIALIZER;
threadData.junk = 0;
pthread_create(&threadProducent, NULL, funProducent, &threadData);
pthread_create(&threadKonzument, NULL, funKonzument, &threadData);
pthread_join(threadProducent, NULL);
pthread_join(threadKonzument, NULL);
free(threadData.buffer);
pthread_mutex_destroy(&threadData.mutex);
pthread_cond_destroy(&threadData.bufferNotFull);
pthread_cond_destroy(&threadData.bufferNotEmpty);
return 0;
}
When I try to run this code, sometimes it stucks in funKonzument at this line:
pthread_cond_wait(&data->bufferNotEmpty, &data->mutex);
But...when I change condition in funProducent method from:
if( randomValue < 13 )
to
if( randomValue > 8 )
everything works fine. Is anyone able to explain me what magic difference is between this two conditions?
You are probably suffering from spurious wakes and some problem with the junk counter. I just removed that counter and added a cond wait loop function (and a little lock context manager) and then the hangings seems to have stopped.
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <stdexcept>
#include <functional>
#define BUFFER_LIMIT 20
struct struktura{
pthread_mutex_t mutex;
pthread_cond_t bufferNotFull;
pthread_cond_t bufferNotEmpty;
int bufferIndex;
char * buffer;
};
// a lock context manager
class mlock {
pthread_mutex_t* mtx;
public:
mlock(pthread_mutex_t& Mtx) :
mtx(&Mtx)
{
int rv=pthread_mutex_lock(mtx);
if(rv) throw std::runtime_error(std::to_string(rv));
}
mlock(const mlock&) = delete;
mlock(mlock&&) = delete;
mlock& operator=(const mlock&) = delete;
mlock& operator=(mlock&&) = delete;
~mlock() {
pthread_mutex_unlock(mtx);
}
};
// silly loop to take care of spurious wakes
void cwait(pthread_cond_t& c, pthread_mutex_t& m, std::function<bool()> f) {
while(f()) pthread_cond_wait(&c, &m);
}
void* funProducent(void *arg){
struktura* data = static_cast<struktura*>(arg);
int i = 0;
while(i < 100) {
mlock dummy(data->mutex);
cwait(data->bufferNotFull, data->mutex, [&](){return data->bufferIndex == BUFFER_LIMIT - 1;});
int randomValue = (rand() % 20) + 1;
if( randomValue < 13 ){
data->buffer[++data->bufferIndex] = 'a';
printf("%2d : Producent at index %d added %c\n", i, data->bufferIndex, data->buffer[data->bufferIndex]);
i++;
pthread_cond_signal(&data->bufferNotEmpty);
}
}
printf("producent is done\n");
return nullptr;
}
void* funKonzument(void *arg){
struktura* data = static_cast<struktura*>(arg);
int i = 0;
while(i < 100) {
mlock dummy(data->mutex);
cwait(data->bufferNotEmpty, data->mutex, [&](){return data->bufferIndex<0;});
printf("\t\t\t%2d : Konzument at index %d consumed %c\n", i, data->bufferIndex, data->buffer[data->bufferIndex]);
data->bufferIndex--;
i++;
pthread_cond_signal(&data->bufferNotFull);
}
printf("\t\t\tkonzument is done\n");
return nullptr;
}
int main() {
pthread_t threadProducent, threadKonzument;
struktura threadData;
threadData.buffer = (char *) malloc(sizeof(char) * BUFFER_LIMIT);
threadData.bufferIndex = -1;
threadData.bufferNotFull = PTHREAD_COND_INITIALIZER;
threadData.bufferNotEmpty = PTHREAD_COND_INITIALIZER;
threadData.mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_create(&threadProducent, NULL, funProducent, &threadData);
pthread_create(&threadKonzument, NULL, funKonzument, &threadData);
pthread_join(threadProducent, NULL);
pthread_join(threadKonzument, NULL);
free(threadData.buffer);
pthread_mutex_destroy(&threadData.mutex);
pthread_cond_destroy(&threadData.bufferNotFull);
pthread_cond_destroy(&threadData.bufferNotEmpty);
return 0;
}

Why am I getting a segmentation fault error?

I am receiving a segmentation fault and am unsure what that means and cant find whats wrong?
Pico is giving this error,
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
//Struct groups each line in the file
struct gradesRecord
{
int iIndex; // index on the file
int iStudentNUM; // 'Student' field
int iExamGrouped[3]; // 'Exam 1'..'Exam 3' fields
char cStudentAVG; // 'Grade' field
};
void printUnsortedStringFromFile(int amount, struct gradesRecord A[]);
void printSortedStringFromFile(int amount, struct gradesRecord A[]);
//bool binSearchNUM(int amount, int A[amount], int target,);
int main()
{
FILE* spData = fopen("grades.csv", "r");
int ch, number_of_lines = 0;
do
{
ch = fgetc(spData);
if (ch == '\n')
number_of_lines++;
} while (ch != EOF);
if (ch != '\n' && number_of_lines != 0)
number_of_lines++;
fclose(spData);
printf("There are %d lines in file grades.csv . \n", number_of_lines);
int amount = number_of_lines;
struct gradesRecord A[amount];
printUnsortedStringFromFile(amount, A );
printSortedStringFromFile(amount, A );
//binSearchNUM(int amount, int A[amount], int target, );
return 0;
}
/*
* Function Name: printUnsortedStringFromFile
*
* Input Parameters: takes array A
*
* Description: This fuction prints the original list that was unsorted in grades.csv
*
* Return Value: void
*/
void printUnsortedStringFromFile(int amount, struct gradesRecord A[])
{
FILE *spData;
spData = fopen("grades.csv", "r");
if(spData == NULL)
{
fprintf(stderr, "Error opening the file grades.csv.\n");
exit(1);
}
printf("+-------+------+------+------+-----+\n");
printf("|Student|Exam 1|Exam 2|Exam 3|Grade|\n");
printf("+-------+------+------+------+-----+\n");
char sLine[81]; //local string to read one row
int j = 0; //storage index
while((fgets(sLine, 80, spData)) != NULL)
{
sscanf(sLine, "%d, %d, %d, %d, %c",
&(A[j].iStudentNUM), &(A[j].iExamGrouped[0]), &(A[j].iExamGrouped[1]), &(A[j].iExamGrouped[2]),
&(A[j].cStudentAVG));
printf("|%7d| %5d| %5d| %5d| %c| \n",
A[j].iStudentNUM, A[j].iExamGrouped[0], A[j].iExamGrouped[1], A[j].iExamGrouped[2],
A[j].cStudentAVG);
j++; // next row
}
printf("+-------+------+------+------+-----+\n");
if (fclose(spData) == EOF)
{
fprintf(stderr, "Error closing the file grades.csv. \n");
exit(2);
}
}
/*
* Function Name: printSortedStringFromFile
*
* Input Parameters: takes int amount, struct gradesRecord A
*
* Description: This function prints the sorted version of the file grades.csv omitting
* the exam values and giving each string a index number
*
* Return Value: void
*/
void printSortedStringFromFile(int amount, struct gradesRecord A[])
{
FILE *spData;
spData = fopen("grades.csv", "r");
if(spData == NULL)
{
fprintf(stderr, "Error opening the file grades.csv.\n");
exit(1);
}
char sLine[81];
int iLine = 0, iRow;
struct gradesRecord grRow, grTmp;
while((fgets(sLine, 80, spData)) != NULL)
{
// extract one Row and store it into grRow
sscanf(sLine, "%d, %d, %d, %d, %c",
&(grRow.iStudentNUM), &(grRow.iExamGrouped[0]), &(grRow.iExamGrouped[1]), &(grRow.iExamGrouped[2]),
&(grRow.cStudentAVG));
// keep the line index of that row
grRow.iIndex = iLine;
// target loop = insertion sort algorithm
for (iRow = 0; iRow < iLine; iRow++)
{
// detect if new student is before the store one
if (grRow.iStudentNUM < A[iRow].iStudentNUM)
{
// exchange both student records through grTmp
memcpy(&grTmp, &(A[iRow]), sizeof(struct gradesRecord));
memcpy(&(A[iRow]), &grRow, sizeof(struct gradesRecord));
memcpy(&grRow, &grTmp, sizeof(struct gradesRecord));
}
}
// store the biggest student at the end
memcpy(&(A[iLine]), &grRow, sizeof(struct gradesRecord));
iLine++;
}
int StudentNUM;
char StudentAVG;
int j = 0;
printf("+-----+-------+-----+\n");
printf("|Index|Student|Grade|\n");
printf("+-----+-------+-----+\n");
int index;
while (j < amount - 1)
{
StudentNUM = A[j].iStudentNUM;
StudentAVG = A[j].cStudentAVG;
index = j+1;
printf("| %4d|%7d| %c| \n", index, StudentNUM, StudentAVG);
j++;
}
printf("+-----+-------+-----+\n");
if (fclose(spData) == EOF)
{
fprintf(stderr, "Error closing the file grades.csv. \n");
exit(2);
}
}
create grades.csv with this input format:
Student,Exam 1,Exam 2,Exam 3,Grade
for example:
1,18,17,14,18
2,15,18,10,16
and output is, witout any Error, but if grades.csv not exist or have a bad information, you get Segmentation fault (core dumped)
There are 3 lines in file grades.csv .
+-------+------+------+------+-----+
|Student|Exam 1|Exam 2|Exam 3|Grade|
+-------+------+------+------+-----+
| 1| 18| 17| 14| 1|
| 2| 15| 18| 10| 1|
+-------+------+------+------+-----+
+-----+-------+-----+
|Index|Student|Grade|
+-----+-------+-----+
| 1| 1| 1|
| 2| 2| 1|
+-----+-------+-----+

IPC_RMID not work on linux with C++

I'm trying to solve my school project in C++. I have to create 15 processes and they have to run in order what means that processes run in this order 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0. It work but when I try to remove semaphore from the memory I am getting error from semctl. On the end I use "semctl(semid, 0, IPC_RMID, 0" but I get error 22 which means EINVAL but it doesn't make sense and I try to remove semaphore from parrent process so I should have privileges to do that.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
#include <sys/wait.h>
union semun {
int val;
struct semid_ds *buf;
ushort *array;
};
struct sembuf sops[1];
int semid;
int wait_sem(int index, int pid){
fprintf(stderr, "\n------- Proces %d do operation wait (-1) on semaphore %d\n",pid, index);
sops[0].sem_num = index;
sops[0].sem_op = -1;
sops[0].sem_flg = 0 ;
if (semop(semid, sops, 1)<0){
perror("semop fail wait");
return 1;
}
else
return 0;
}
int signal_sem(int index, int pid){
fprintf(stderr, "\n++++++ Proces %d vykonava operaciu signal (1) na semafore %d\n",pid,index);
sops[0].sem_num = index;
sops[0].sem_op = 1;
sops[0].sem_flg = 0;
if (semop(semid, sops, 1)<0){
perror("semop fail signal");
return 1;
}
else
return 0;
}
void createSem(key_t paKey, int paSemFlg, int paNsems)
{
printf ("uid=%d euid=%d\n", (int) getuid (), (int) geteuid ());
(semid = semget(paKey, paNsems, paSemFlg));
for (int i = 0; i < paNsems; ++i) {
semctl(semid, i, SETVAL, 0);
}
}
void kic()
{
printf("\naaaaaaaaaaaaaa\n");
}
int main() {
key_t key = 1234;
int semflg = IPC_CREAT | 0666;
int nsems = 15;
int semid;
fprintf(stderr, "%d=", sops);
createSem(IPC_PRIVATE, semflg, nsems);
if (semid == -1) {
perror("semget: semget failed");
return 1;
}
else
fprintf(stderr, "semget: semget sucess: semid = %d, parrent pid %d\n", semid, getpid());
int PROCESS_ID = 0;
pid_t PID;
for (int i = 1; i < nsems; i++) {
PID = fork();
if(PID == 0)
{
PROCESS_ID = i;
break;
}
}
if(PID == -1)
{
printf("\nPID ERROR");
}
if(PID != 0) //parrent
{
printf("\n\nparrent with ID %d", PROCESS_ID);
signal_sem(PROCESS_ID+1, PROCESS_ID);
wait_sem(PROCESS_ID, PROCESS_ID);
printf ("uid=%d euid=%d\n", (int) getuid (), (int) geteuid ());
printf("\nEND %d\n", getpid());
int s;
wait(&s);
if((semctl(semid, 0, IPC_RMID, 0))==-1)
{
int a = errno;
printf("\nERROR IPC_RMID %d\n", a);
}
}
if(PID == 0)//child
{
if(wait_sem(PROCESS_ID, PROCESS_ID) == 0){
printf("\nI am child with ID %d", PROCESS_ID);
int ID_NEXT_PROCESS = 1+PROCESS_ID;
if(ID_NEXT_PROCESS == nsems)
ID_NEXT_PROCESS = 0;
signal_sem(ID_NEXT_PROCESS, PROCESS_ID);
return 0;
}
}
return 0;
}
You have two semids. One in global scope, another local to main (which shadows global, you should see a warning). createSem only knows about global one, and initializes it. semctl is called directly by main, and is passed the local one, which is garbage.

Multi-threading 4 Accelerometers - Data Unstable

I just started learning C++ this year. Currently, I'm using four accelerometers to calculate human body movement for my school project. I tried to use multi-thread but the data outputs are unstable as such when I tried multiple times, some of the data would change. It does give correct output data sometimes. The output is saved to Microsoft Excel while printing on the screen simultaneously. However, When I tried the accelerometer separately, it seems to work just fine. The output would change only when I multi-thread them together. I can't seem to find what cause the output data to change periodically. I appreciate any help I could get. Pardon my grammatical mistakes for it is not my native language. Thank you =D
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <process.h>
#include <Windows.h>
#include "erslib.h"
struct SENSOR_DATA {
int ID_1, ID_2, ID_3,ID_4;
int y_1, y_2, y_3, y_4;
int z_1, z_2, z_3, z_4;
int x_1, x_2, x_3, x_4;
};
void Thread_0(void);
void Thread_1(void);
void Thread_2(void);
void Thread_3(void);
FILE *fp[4];
errno_t err[4];
int key = 0;
int MainLoopFlag = true;
SENSOR_DATA acc;
int main()
{
DWORD ThreadID_0;
DWORD ThreadID_1;
DWORD ThreadID_2;
DWORD ThreadID_3;
HANDLE Handle_0 = NULL;
HANDLE Handle_1 = NULL;
HANDLE Handle_2 = NULL;
HANDLE Handle_3 = NULL;
Handle_0 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_0, 0, 0, &ThreadID_0);
Sleep(1000);
Handle_1 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_1, 0, 0, &ThreadID_1);
Sleep(1000);
Handle_2 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_2, 0, 0, &ThreadID_2);
Sleep(1000);
Handle_3 = CreateThread(0,0,(LPTHREAD_START_ROUTINE) Thread_3, 0, 0, &ThreadID_3);
Sleep(1000);
WaitForSingleObject(Thread_0, INFINITE);
WaitForSingleObject(Thread_1, INFINITE);
WaitForSingleObject(Thread_2, INFINITE);
WaitForSingleObject(Thread_3, INFINITE);
//////Print and Data Output//////
while(MainLoopFlag){
if(_kbhit()){
key = _getch(); //a key to start data saving//
if(key == 'a' && Save_flag==0){
printf("Preparing for saving...\n");
Sleep(5000);
printf("Saving start\n");
char * fname_0 = ("C:\\Users\\Desktop\\adam\\ID 1\\test02.csv");
err[0] = fopen_s(&fp[0], fname_0,"w+");
if (fp[0]==NULL) {
printf("%s File cannot be opened。\n",fname_0);
return -1;
}
char * fname_1 = ("C:\\Users\\Desktop\\adam\\ID 2\\test02.csv");
err[1] = fopen_s(&fp[1], fname_1,"w+");
if (fp[1]==NULL) {
printf("%s File cannot be opened。\n",fname_1);
return -1;
}
char * fname_2 = ("C:\\Users\\Desktop\\adam\\ID 3\\test02.csv");
err[2] = fopen_s(&fp[2], fname_2,"w+");
if (fp[2]==NULL) {
printf("%s File cannot be opened。\n",fname_2);
return -1;
}
char * fname_3 = ("C:\\Users\\Desktop\\adam\\ID 4\\test02.csv");
err[3] = fopen_s(&fp[3], fname_3,"w+");
if (fp[3]==NULL) {
printf("%s File cannot be opened。\n",fname_3);
return -1;
}
Save_flag=1;
while(MainLoopFlag && Save_flag==1){
printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_1,acc.x_1,acc.y_1,acc.z_1);
fprintf(fp[0],"%d, %d, %d, %d \n",acc.ID_1,acc.x_1,acc.y_1,acc.z_1);
printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_2,acc.x_2,acc.y_2,acc.z_2);
fprintf(fp[1],"%d, %d, %d, %d \n",acc.ID_2,acc.x_2,acc.y_2,acc.z_2);
printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_3,acc.x_3,acc.y_3,acc.z_3);
fprintf(fp[2],"%d, %d, %d, %d \n",acc.ID_3,acc.x_3,acc.y_3,acc.z_3);
printf("ID->%d, x->%d, y->%d, z->%d \n",acc.ID_4,acc.x_4,acc.y_4,acc.z_4);
fprintf(fp[3],"%d, %d, %d, %d \n",acc.ID_4,acc.x_4,acc.y_4,acc.z_4);
if(_kbhit()){
key = _getch();
//s key to stop data saving
if(key == 's' && Save_flag==1){
printf("End Data saving\n");
Save_flag=0;
fclose(fp[0]);
fclose(fp[1]);
fclose(fp[2]);
fclose(fp[3]);
MainLoopFlag = false;}
}
}
}
}
}
// Close Handle
CloseHandle(Handle_0);
CloseHandle(Handle_1);
CloseHandle(Handle_2);
CloseHandle(Handle_3);
return 0;
}
//--------------------------------------------------------------------------
// Name: Thread_1(void)
//--------------------------------------------------------------------------
void Thread_1(void)
{
unsigned char buf[4096];
unsigned char buf_data[4096];
unsigned char bin_data[5];
//unsigned char buf_str[4096];
int i,j,n;
int n_cou;
int ComPort = 20;
int flag=0;
int temp_char_1[7];
ERS_Open(ComPort,4096,4096);
ERS_Config(ComPort,ERS_38400|ERS_NO|ERS_1|ERS_8);
//最初に取得するデータは異常なので捨てる
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
// 1となる位置を調べる
flag=0;
while(MainLoopFlag){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++){
if((buf[i] & 0x80) == 0x80){
flag=1;
break;
}
}
if(flag==1)break;
Sleep(1);
}
// 1 となる位置から後ろをbuf_dataに入れる
n_cou = n - i;
for(j=0;j<n_cou;j++)
buf_data[j]=buf[i+j];
while(MainLoopFlag){
while(MainLoopFlag && n_cou<5){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++)buf_data[n_cou+i]=buf[i];
n_cou=n_cou+n;
}
for(i=0;i<5;i++)bin_data[i]=buf_data[i];
temp_char_1[0] = (bin_data[0] & 0x38) >> 3; //ID
acc.ID_2 = (int)temp_char_1[0];
temp_char_1[1] = (bin_data[0] & 0x07) << 7; // x = 3bit
temp_char_1[2] = (bin_data[1] & 0x38) << 4; // y = 3bit
temp_char_1[3] = (bin_data[1] & 0x07) << 7; // z = 3bit
temp_char_1[4] = (bin_data[2] & 0x7F) | temp_char_1[1];
acc.x_2 = (int)temp_char_1[4];
temp_char_1[5] = (bin_data[3] & 0x7F) | temp_char_1[2];
acc.y_2 = (int)temp_char_1[5];
temp_char_1[6] = (bin_data[4] & 0x7F) | temp_char_1[3];
acc.z_2 = (int)temp_char_1[6];
for(i=5;i<n_cou;i++)buf_data[i-5]=buf_data[i];
n_cou=n_cou-5;
}
ERS_CloseAll();
}
//--------------------------------------------------------------------------
// Name: Thread_2(void)
//--------------------------------------------------------------------------
void Thread_2(void)
{
unsigned char buf[4096];
unsigned char buf_data[4096];
unsigned char bin_data[5];
//unsigned char buf_str[4096];
int i,j,n;
int n_cou;
int ComPort = 15;
int flag=0;
int temp_char_2[7];
ERS_Open(ComPort,4096,4096);
ERS_Config(ComPort,ERS_38400|ERS_NO|ERS_1|ERS_8);
//最初に取得するデータは異常なので捨てる
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
// 1となる位置を調べる
flag=0;
while(MainLoopFlag){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++){
if((buf[i] & 0x80) == 0x80){
flag=1;
break;
}
}
if(flag==1)break;
Sleep(1);
}
// 1 となる位置から後ろをbuf_dataに入れる
n_cou = n - i;
for(j=0;j<n_cou;j++)
buf_data[j]=buf[i+j];
while(MainLoopFlag){
while(MainLoopFlag && n_cou<5){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++)buf_data[n_cou+i]=buf[i];
n_cou=n_cou+n;
}
for(i=0;i<5;i++)bin_data[i]=buf_data[i];
temp_char_2[0] = (bin_data[0] & 0x38) >> 3; //ID
acc.ID_3 = (int)temp_char_2[0];
temp_char_2[1] = (bin_data[0] & 0x07) << 7; // x = 3bit
temp_char_2[2] = (bin_data[1] & 0x38) << 4; // y = 3bit
temp_char_2[3] = (bin_data[1] & 0x07) << 7; // z = 3bit
temp_char_2[4] = (bin_data[2] & 0x7F) | temp_char_2[1];
acc.x_3 = (int)temp_char_2[4];
temp_char_2[5] = (bin_data[3] & 0x7F) | temp_char_2[2];
acc.y_3 = (int)temp_char_2[5];
temp_char_2[6] = (bin_data[4] & 0x7F) | temp_char_2[3];
acc.z_3 = (int)temp_char_2[6];
for(i=5;i<n_cou;i++)buf_data[i-5]=buf_data[i];
n_cou=n_cou-5;
}
ERS_CloseAll();
}
//--------------------------------------------------------------------------
// Name: Thread_3(void)
//--------------------------------------------------------------------------
void Thread_3(void)
{
unsigned char buf[4096];
unsigned char buf_data[4096];
unsigned char bin_data[5];
//unsigned char buf_str[4096];
int i,j,n;
int n_cou;
int ComPort = 3;
int flag=0;
int temp_char_3[7];
ERS_Open(ComPort,4096,4096);
ERS_Config(ComPort,ERS_38400|ERS_NO|ERS_1|ERS_8);
//最初に取得するデータは異常なので捨てる
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
// 1となる位置を調べる
flag=0;
while(MainLoopFlag){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++){
if((buf[i] & 0x80) == 0x80){
flag=1;
break;
}
}
if(flag==1)break;
Sleep(1);
}
// 1 となる位置から後ろをbuf_dataに入れる
n_cou = n - i;
for(j=0;j<n_cou;j++)
buf_data[j]=buf[i+j];
//for(i=0;i<n_cou;i++)printf("%x ",buf_data[i]);
//printf("\n");
while(MainLoopFlag){
while(MainLoopFlag && n_cou<5){
n=ERS_CheckRecv(ComPort);
ERS_Recv(ComPort,buf,n);
for(i=0;i<n;i++)buf_data[n_cou+i]=buf[i];
n_cou=n_cou+n;
}
for(i=0;i<5;i++)bin_data[i]=buf_data[i];
//for(i=0;i<5;i++)printf("%x ",bin_data[i]);
//printf("\n");
temp_char_3[0] = (bin_data[0] & 0x38) >> 3; //ID
acc.ID_4 = (int)temp_char_3[0];
temp_char_3[1] = (bin_data[0] & 0x07) << 7; // x = 3bit
temp_char_3[2] = (bin_data[1] & 0x38) << 4; // y = 3bit
temp_char_3[3] = (bin_data[1] & 0x07) << 7; // z = 3bit
temp_char_3[4] = (bin_data[2] & 0x7F) | temp_char_3[1];
acc.x_4 = (int)temp_char_3[4];
temp_char_3[5] = (bin_data[3] & 0x7F) | temp_char_3[2];
acc.y_4 = (int)temp_char_3[5];
temp_char_3[6] = (bin_data[4] & 0x7F) | temp_char_3[3];
acc.z_4 = (int)temp_char_3[6];
for(i=5;i<n_cou;i++)buf_data[i-5]=buf_data[i];
n_cou=n_cou-5;
}
ERS_CloseAll();
}
Correct data :
(Example)
ID->1, x->47, y->147, z->298
ID->2, x->298, y->25, z->147
ID->3, x->47, y->147, z->298
ID->4, x->213, y->123, z->43
ID->1, x->49, y->152, z->222
ID->2, x->256, y->30, z->155
ID->3, x->47, y->147, z->298
ID->4, x->221, y->132, z->54
incorrect data:
ID->1, x->905, y->179, z->20
ID->6, x->47, y->147, z->298
ID->0, x->0, y->0, z->0
ID->4, x->1010, y->56, z->23
ID->1, x->905, y->179, z->20
ID->6, x->47, y->147, z->298
ID->0, x->0, y->0, z->0
ID->4, x->1010, y->56, z->23
Basically the ID shouldn't change while the x, y and z would as you move the accelerometer.