C 如何使子线程等待主线程

C 如何使子线程等待主线程,c,multithreading,C,Multithreading,我有两个线程:一个子线程检测鼠标事件,另一个主线程执行程序 全局变量: 子线程: 主线程: 我的问题是:当我的主线程正在等待时,我的子线程正在使用100%1核心处理器,那么我可以使用哪个函数将子线程与主线程暂停 我已经咨询过了,但它并没有完全帮助我。所以,如果您想暂停主线程,直到子线程发出关于它结束的信号,您可以使用互斥锁来锁定主线程: #include "stdio.h" #include "pthread.h" /* I work on windows now, so i need win

我有两个线程:一个子线程检测鼠标事件,另一个主线程执行程序

全局变量:

子线程:

主线程:

我的问题是:当我的主线程正在等待时,我的子线程正在使用100%1核心处理器,那么我可以使用哪个函数将子线程与主线程暂停


我已经咨询过了,但它并没有完全帮助我。

所以,如果您想暂停主线程,直到子线程发出关于它结束的信号,您可以使用互斥锁来锁定主线程:

#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 */
}

您可以使用a来减轻cpu的负载。@Sanderedycker您使用多少时间来减轻cpu的负载?我用500毫秒进行测试,我的cpu总是在25%使用(使用四核)@sanderedycker我在我的程序中添加了follinwing行:
if(g_wait){struct timespec waiting;waiting.tv_sec=0;waiting.tv_nsec=500000;nanosleep(&waiting,NULL)}
您可以使用,我认为g_wait应该是一个原子,“等待主程序”是什么意思?在允许子线程停止等待之前,您希望主线程做什么?在我的子线程中有一个
while
功能,因此我不确定我是否能够像那样使用
pthread\u mutex\u unlock
,是我的用户决定暂停并恢复程序,不是定义等待时间的任意值time@damadam,pause只是一个例子,当然你可以使用循环和其他东西。
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);
}
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;
}
#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 */
}