10 years, 5 months ago.

Multiple threads

Hey guys , I am just trying to create a multi-thread program so I can send and receive information from my setup but for some reason the threads do not start or at least they dont seem to start.

Main.cpp

#include "mbed.h"
#include "BurstSPI.h"
#include "rtos.h"


Serial pc(USBTX, USBRX);
AnalogIn Vin(p18);
DigitalOut CS(p35);
DigitalOut LDR(p30);
int output=0x0000 ;
int next=1;
int i=0;
int data=0x0000;
BurstSPI device(p5, p6, p7); // mosi, miso, sclk
void outputSignal(void const *args)
{
    device.format(16,0);
    device.frequency(48000000);
    device.setFormat();
    LDR=1;

    while(1) {
        CS=0;

        data=output|0x3000;
        device.fastWrite(data);
        device.clearRX();

        CS=1;
        if (next==1) {
            output=output+0x000F;
        } else if (next==2) {
            output=output-0x000F;
        }
        if (output>=0x0fff) {
            //output=0x0000;
            next=2;
        } else if (output<=0x0000) {
            next=1;
        }
        
        i++;
        LDR=0;
        LDR=1;
    }
}


//osThreadDef(led2_thread, osPriorityNormal, DEFAULT_STACK_SIZE);

///the main method
int main()
{
  Thread testSignal(outputSignal);
    //device.format(16,0);
//    device.frequency(48000000);
//    device.setFormat();
//    LDR=1;
//
//    while(1) {
//        CS=0;
//
//        data=output|0x3000;
//        device.fastWrite(data);
//        device.clearRX();
//
//        CS=1;
//        if (next==1) {
//            output=output+0x000F;
//        } else if (next==2) {
//            output=output-0x000F;
//        }
//        if (output>=0x0fff) {
//            //output=0x0000;
//            next=2;
//        } else if (output<=0x0000) {
//            next=1;
//        }
//        
//        i++;
//        LDR=0;
//        LDR=1;

}
}

1 Answer

10 years, 5 months ago.

As a start, I think you need to have an infinite loop inside main(). Otherwise, main(), and thus your program, exits immediately after starting your thread.

Something like:

int main() {

   Thread testSignal(outputSignal);

    while (true) {
    }
}

Accepted Answer

The truth is , that this is the first thing I did myself , but it didnt seem to have any effect . But I tested it when the program itself was a bit different . I will try again .

posted by Antreas Antoniou 03 Jan 2014

Better then also disable the main thread, either by letting it wait on a signal it will never get, or just using a regular RTOS wait. Then it won't use CPU time on an infinite loop.

posted by Erik - 03 Jan 2014

Seems like you might also want to have your outputSignal thread do a signal_wait() or wait().

posted by David G 03 Jan 2014

I just tried the wait technique in my main loop . It seems to work now , I mean the second thread is running but the triangles I output are only there half of the time and the signal comes out really bad . For example half the waveform is there and the other half is like a shadow that is there for a very brief amount of time. Why is this ? I am using a wait without the while loop .

posted by Antreas Antoniou 06 Jan 2014

Correction , I was using wait right before the while loop that I assumed would never be reached because I used wait for ever . By removing the loop and leaving only wait for ever it seems that my main loop reaches the end and stops the whole thing again . How can I get the main loop to stop doing whatever its doing and concentrate on the new thread?

posted by Antreas Antoniou 06 Jan 2014

Progress . I managed to get a nice smooth signal using priorityHigh . Is there another way to make this work , just for future reference .

posted by Antreas Antoniou 06 Jan 2014

Excuse me there guys , If anybody still checks this question out . Can you explain what priorities really do ? I have two threads one with high priority and one with normal and the one with normal priority does not execute at all . I was expecting the lower priority thread to be slower than the high priority and instead I don't see it working at all .

posted by Antreas Antoniou 07 Jan 2014