0

i am currently working on raspberry pi and interfacing an ADC with it. The ADC outputs the digital value continuously. The reading of the value and its processing is initiated and executed in a thread, therefore it should run forever. However in order to exit (for some reason) from the thread cleanly, I want to use SIGINT signal handler, which triggers an interrupt and changes the state of volatile variable. The pseudo-program is as follows:

volatile sig_atomic_t exitThread = 0;

void signalHandler(){
    exitThread = 1;
}

void SPI_Port0(){
    //initialisation of functions and variables
    
    while(!exitThread){
         //do the work
    }

    if(exitThread){
         pthread_exit(NULL);
    }
}

void main(){

     struct sigaction sa;
     memset(&sa, 0, sizeof(sa));
     sigemptyset(&sa.sa_mask);
     sa.sa_handler = signalHandler;
     
     sigaction(SIGINT, &sa, NULL);

     pthread_t SPI0,SPI3;
     pthread_create(&SPI0, NULL, SPI_Port0, NULL);
     pthread_create(&SPI3, NULL, SPI_Port1, NULL);

     pthread_join(SPI0, NULL);
     pthread_join(SPI3, NULL);

   //printing the results stored in arrays
   //the arrays is declared global
 }

there are two threads SPI_Port0 and SPI_Port1 doing the same implementation but on two different SPI ports, I want these two threads to exit when the CTRL + C is pressed. The problem i am facing is that, the whole program is exited.

Can someone point me in the right direction. Any help would be appreciated.

  • 1
    Are those two threads the only threads running? If they are, and they both exit on CTRL-C, then the entire program will exit since both `pthread_join()` calls will return and the main thread will continue. Is what you've posted complete? Are there other threads? – Andrew Henle Jun 16 '21 at 13:54
  • This probably isn't your immediate problem, but the `if (exitThread) { pthread_exit }` block is unnecessary -- returning from a thread procedure is equivalent to calling pthread_exit. – zwol Jun 16 '21 at 14:27
  • And this probably isn't your immediate problem _either_, but your thread procedures have the wrong function signature -- they should be declared as e.g. `void *SPI_Port0(void *unused)` and they should end with `return 0;`. – zwol Jun 16 '21 at 14:28
  • @AndrewHenle actually yes. After i hit ctrl + c, the threads should exit and the processing should move on to main function. After the join_thread call, I actually want to print the processed data values, which i have stored in an array (globally declared) . But as soon as i hit ctrl + c, the whole program exits. – Kaustubh Bhargava Jun 16 '21 at 14:47
  • @zwol thank you for pointing out that thing. I actually went through numerous posts on how to declare thread functions i saw this on one of them. I ll correct it. – Kaustubh Bhargava Jun 16 '21 at 14:57
  • The skeleton you've presented looks healthy: you set SIGINT disposition to a handler that sets a flag. Whatever else, the INT shouldn't have its default, process-termination behavior. Can you give us a [mre], perhaps in which the worker threads (_both_ of them, please) simply sleep? – pilcrow Jun 16 '21 at 17:26
  • I'm a bit confused. What do you want the program to do? It's currently coded to exit and has no other code it can execute. – David Schwartz Jun 16 '21 at 20:40
  • @DavidSchwartz In the thread i am storing the processed values in a global declared structured array. So as soon as the thread exits, i want to access those values in global array in my main and print them for analysis. – Kaustubh Bhargava Jun 17 '21 at 12:04
  • @KaustubhBhargava So write some code to do that. What's the question? – David Schwartz Jun 21 '21 at 17:14
  • @DavidSchwartz I have done that. The problem is that thing is not being processed in the code. Once the thread exits, the cursor should come in the main and execute that for loop displaying the results stored in the array. SO the question is why ? – Kaustubh Bhargava Jun 22 '21 at 14:45

1 Answers1

-2

Modifications of variable and globals are not shared with child threads because they are other processes.

Fisrt of all you have to catch the signal : Catch Ctrl-C in C

Then, in the signal interrupt, the child thread must be killed: For pthread, How to kill child thread from the main thread

ebuckis
  • 27
  • 3
  • *Variable and globals are not shared with child threads because they are other processes.* What?!?!? – Andrew Henle Jun 16 '21 at 14:21
  • the globals variable are shared among threads. please go through this link once:https://www.geeksforgeeks.org/multithreading-c-2/#:~:text=POSIX%20Threads%20(or%20Pthreads)%20is,is%20available%20with%20gcc%20compiler.&text=Please%20note%20that%20the%20below,C%20compilers%20with%20pthread%20library.&text=%23include%20%3Cunistd. – Kaustubh Bhargava Jun 16 '21 at 14:57
  • @KaustubhBhargava Global variables _are_ shared among all threads, but geeksforgeeks.org is not a reliable reference, please don't point people at it. I see many errors on the page you linked to. – zwol Jun 16 '21 at 15:26