Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何阻止pthread从main打印全局变量,然后继续pthread?_C_Multithreading_Pthreads_Mutex_Semaphore - Fatal编程技术网

C 如何阻止pthread从main打印全局变量,然后继续pthread?

C 如何阻止pthread从main打印全局变量,然后继续pthread?,c,multithreading,pthreads,mutex,semaphore,C,Multithreading,Pthreads,Mutex,Semaphore,我有一个创建pthread的主函数。我正在尝试使用信号量(注意-mutex、s1、s2和memoryUsed都是全局声明的)在特定时间打印全局变量: 在我的目标函数中,我想在一个选择点停止,并从main打印一个全局变量。注意,变量在目标函数中已更改。打印后,我想继续执行目标功能: void *matrix_management(void *len) { pthread_mutex_lock(&mutex); // CHANGE GLOBAL VAR memory

我有一个创建pthread的主函数。我正在尝试使用信号量(注意-mutex、s1、s2和memoryUsed都是全局声明的)在特定时间打印全局变量:

在我的目标函数中,我想在一个选择点停止,并从main打印一个全局变量。注意,变量在目标函数中已更改。打印后,我想继续执行目标功能:

void *matrix_management(void *len)
{
    pthread_mutex_lock(&mutex);

    // CHANGE GLOBAL VAR
    memoryUsed = 1;

    sem_post(&s1); // print from main
    sem_wait(&s2); // wait for signal

    // continue thread...


    pthread_mutex_unlock(&mutex);
    return NULL;
}

但是,主线程似乎根本没有等待来自pthread的信号。它会立即打印全局变量的值。如果我注释掉pthread中的信号,程序不会死锁。您可以像这样在主线程和pthread之间使用信号量吗?

根据您的平台,可能不支持未命名的信号量。在这种情况下,可以使用以下方法,不过您可能希望创建临时名称,并在以后用于取消链接。我熟悉这一点的唯一原因是,我通常使用的平台OS X不支持10.8.3中未命名的信号量,因此,无错误检查我与您的代码有类似的行为,因为所有信号量API都失败了

希望能有帮助

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

sem_t* s1=NULL, *s2=NULL;
int memoryUsed = 0;

void *matrix_management(void *len)
{
    // CHANGE GLOBAL VAR
    memoryUsed = 1;

    sem_post(s1); // print from main
    sem_wait(s2); // wait for signal
    printf("Thread woken up.\n");

    // continue thread...
    return NULL;
}

int main(int argc, const char * argv[])
{
    s1 = sem_open("s1", O_CREAT, S_IRUSR | S_IWUSR, 0);
    s2 = sem_open("s2", O_CREAT, S_IRUSR | S_IWUSR, 0);
    assert(s1 && s2 && "Failed to allocate semaphores.");

    pthread_t t0;
    pthread_create(&t0, NULL, &matrix_management, NULL);

    sem_wait(s1); // wait for signal from pthread
    printf("Memory Usage from main: %d bytes.\n", memoryUsed); // global var

    printf("Waking thread...\n"); // global var
    sem_post(s2); // signal pthread to continue

    pthread_join(t0, NULL);
    printf("Thread finished\n");
    sem_close(s1);
    sem_close(s2);
    sem_unlink("s1");
    sem_unlink("s2");
    return 0;
}

这确实是问题所在。我不知道关于OSX的事。谢谢你的帮助!
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <assert.h>

sem_t* s1=NULL, *s2=NULL;
int memoryUsed = 0;

void *matrix_management(void *len)
{
    // CHANGE GLOBAL VAR
    memoryUsed = 1;

    sem_post(s1); // print from main
    sem_wait(s2); // wait for signal
    printf("Thread woken up.\n");

    // continue thread...
    return NULL;
}

int main(int argc, const char * argv[])
{
    s1 = sem_open("s1", O_CREAT, S_IRUSR | S_IWUSR, 0);
    s2 = sem_open("s2", O_CREAT, S_IRUSR | S_IWUSR, 0);
    assert(s1 && s2 && "Failed to allocate semaphores.");

    pthread_t t0;
    pthread_create(&t0, NULL, &matrix_management, NULL);

    sem_wait(s1); // wait for signal from pthread
    printf("Memory Usage from main: %d bytes.\n", memoryUsed); // global var

    printf("Waking thread...\n"); // global var
    sem_post(s2); // signal pthread to continue

    pthread_join(t0, NULL);
    printf("Thread finished\n");
    sem_close(s1);
    sem_close(s2);
    sem_unlink("s1");
    sem_unlink("s2");
    return 0;
}