0

in this code, I am creating two threads (joystick and motor). The intention of the joystick thread is to ask the user to input a certain speed integer value (for eg. 200). From my understanding, this integer value must be converted to a string in-order for the motor thread to receive this string value and convert it back to an integer value, before passing it into my application.

The result of the compiled code in GCC is my motor application receives a speed of zero despite a user input of any other numbers, and hence, it is not moving. May I know if I had done the conversion from integer to string or vice versa wrongly?

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

#define MAX_LEN 128

//char n[1024];

pthread_mutex_t lock= PTHREAD_MUTEX_INITIALIZER;
int string_read=FALSE;

pthread_cond_t cond;

void * joystick()
{
    while (1)
    {
        int s;
        char str[7];

        while(string_read);
        pthread_mutex_lock(&lock);

        printf("Enter speed: ");
        scanf("%d", &s);
        snprintf(str, 7, "%d", s);  //Convert int to string: itoa or snprintf

        //itoa (j, n, 10);
        //printf ("Decimal: %s\n", n);

        string_read=TRUE;
        pthread_mutex_unlock(&lock);
        pthread_cond_signal(&cond);
    }
}

void * motor()
{
    while (1)
    {
        pthread_mutex_lock(&lock);
        while (!string_read)
        pthread_cond_wait(&cond,&lock);

        char *s = "s";  //How to receive the string value from joystick()?
        int val = atoi(s);
        printf ("Integer value of string is %d\n", val);

        char buffer [MAX_LEN];
        system ("./SmcCmd --resume");   //WORKING
        snprintf (buffer, MAX_LEN, "./SmcCmd --speed %d", val); //WORKING
        int status = system(buffer);    //WORKING

        string_read=FALSE;
        pthread_mutex_unlock(&lock);
    }
}

int main ()
{
    int status;
    pthread_t tj, tm;
    pthread_create(&tj, NULL, joystick, NULL);
    pthread_create(&tm, NULL, motor, NULL);
    pthread_join(tj, NULL);
    pthread_join(tm, NULL);
    return 0;
}
6a_lau
  • 1
  • 1
    You already have some globals (`cond`, `string_read`) just make the integer value a global too. – Chad Mar 20 '15 at 01:36
  • 1
    Why not just read the input as a sting to begin with? e.g. `scanf ("%[^\n]%*c", str);` Unless I'm missing something, there is no need to read as an `int` and then convert back to `char *`, just read `char *`. – David C. Rankin Mar 20 '15 at 01:38
  • I don't get your question: 1. I have never heard of restriction of only possible to use string as "communication" between threads. 2. if you are fine for that global variable `string_read`, just do the same for the `speed` which is provided by joystick and read by motor. – Adrian Shum Mar 20 '15 at 01:38
  • As described in http://stackoverflow.com/a/4567919/718379 , you should _not_ `pthread_mutex_unlock(&lock);` before `pthread_cond_signal(&cond);`. – timrau Mar 20 '15 at 01:39
  • @Chad Thank you! It was so simple and I did not realise it! Thank you so much! – 6a_lau Mar 20 '15 at 03:03
  • Now that this is solved, I have a logitech joystick which I want to use this as my input 'integer' values as the user moves the joystick forward or backward. Are there any programs to receive the values and pass it to my program above? – 6a_lau Mar 20 '15 at 03:26

0 Answers0