-1

I've got 2 threads : one child which detects mouse events, and the other main one which execute the program.

global variable :

int g_wait = 0; 

child thread :

void *mouseEvent2(void *arg) 
{
int fd;
struct input_event ev;
const char* pFile = "/dev/input/event0";

signal(SIGINT, SAMPLE_VGS_HandleSig);
signal(SIGTERM, SAMPLE_VGS_HandleSig);

fd = open(pFile, O_RDONLY);
if (fd == -1) {
    printf("ERROR Opening %s\n", pFile);
    return NULL;
}

while(scroll != -1) {
    read(fd, &ev, sizeof(ev));

    [... Some code with if statement ... ]
    if(...) //left mouse button
        g_wait = !g_wait;

    [need waiting so the thread won't use at 100% a core]
}
close(fd);
(void) arg;
pthread_exit(NULL);
}

Main thread :

int main() {
[... some code and initilisation here ...]
if(pthread_create(&scrollThread, NULL, mouseEvent2, NULL))
    goto _FAILURE_;
do {
    [...]
    while(g_wait);
}
[... some deinit ...]
_FAILURE_ :
pthread_join(scrollThread, NULL);
[... some other deinit ...]
return 0;
}

My problem is : when my main is waiting, my child thread is using at 100% 1 core processor, so which function can I use to pause the child thread with the main one?

I already consult How to make main thread wait for all child threads finish? but it didn't help me totally.

damadam
  • 330
  • 4
  • 17
  • you can use a [brief sleep](https://stackoverflow.com/questions/7684359/how-to-use-nanosleep-in-c-what-are-tim-tv-sec-and-tim-tv-nsec/7684399) to take the load off the cpu. – Sander De Dycker Jun 13 '18 at 15:03
  • @SanderDeDycker how many time do you use to overload the cpu? I tested with 500 ms, my cpu is always at 25% using (with a quadcore) – damadam Jun 13 '18 at 15:11
  • @SanderDeDycker I added the follinwing lines in my program : `if(g_wait) { struct timespec waiting; waiting.tv_sec = 0; waiting.tv_nsec = 500000; nanosleep(&waiting, NULL); }` – damadam Jun 13 '18 at 15:12
  • you can use [yeild](http://man7.org/linux/man-pages/man3/pthread_yield.3.html) and i think g_wait should be an atomic – Tyker Jun 13 '18 at 15:25
  • What does, "wait for the main program" mean? What is it that you want the main thread to _do_ before the child thread is allowed to stop waiting? – Solomon Slow Jun 13 '18 at 16:09
  • @Tyker `pthread_yield` didn't overload the processor – damadam Jun 14 '18 at 06:35
  • @jameslarge I want to be able to stop temporary the main thread, and this has as consequence of full utilisation of one processor because the child thread isn't waiting -> I would like to add some lines to let the cpu free of charge during this waiting – damadam Jun 14 '18 at 06:38
  • @damadam Why is the thread using 100% of the CPU anyway? What is it doing exactly? The code you've shown should block in `read` until there's data for it to read. If the thread is using 100% of the CPU when it has no work to do, that's likely just some bug in the thread's existing code. If it has nothing to do, what is it doing? – David Schwartz May 04 '20 at 00:51

1 Answers1

0

So, if you want to pause main thread until child thread signals about it ends, you can use mutexes to lock main thread:

#include "stdio.h"
#include "pthread.h"

/* I work on windows now, so i need windows sleep function to test example */
/* platform independed sleep function */
#ifdef _WIN32
# include "windows.h"
# define platform_sleep(ms) Sleep(ms)
#else
# include "unistd.h"
# define platform_sleep(s) sleep(s / 1000)
#endif


pthread_mutex_t mtx;


void *
child_func(void *arg)
{
    /* simulate some hard work */
    platform_sleep(3000); /* 3 secs */

    /* after this "hard work" we allow main thread to continue */
    pthread_mutex_unlock(&mtx);
}



int
main()
{
    pthread_mutex_init(&mtx, NULL);
    pthread_mutex_lock(&mtx);

    pthread_t child;
    pthread_create(&child, NULL, child_func, NULL);

    /* mutex is already locked by main thread, so it waits */
    /* until child thread unlock it */
    pthread_mutex_lock(&mtx);
    pthread_mutex_destroy(&mtx);

    /* do work after child ends */
}
kerrytazi
  • 583
  • 1
  • 4
  • 15
  • there is a `while` funtion in my child thread, so i'm not sure I will be able to use the `pthread_mutex_unlock` like that, and it's my user who decide to pause and resume the program, not an arbitrary value which define the waiting time – damadam Jun 14 '18 at 07:56
  • @damadam, pause is used just for example, of course u can use loops and other stuff. – kerrytazi Jun 14 '18 at 08:13