Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.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++ 互斥体/被锁定的互斥体数据是什么? #包括 #包括 #包括“errors.h” typedef结构报警标签{ 结构报警标签*链接; 整数秒; 从历元算起的时间;/*秒*/ 字符消息[64]; }警报; pthread\u mutex\u t alarm\u mutex=pthread\u mutex\u初始值设定项; 报警*报警列表=空; 无效*报警线程(无效*参数) { 警报_t*警报; 整夜睡眠时间; 现在是时候了; 智力状态; 而(1){ 状态=pthread\u mutex\u lock(&alarm\u mutex); 如果(状态!=0) err_abort(状态为“锁定互斥”); 报警=报警列表; /* *如果报警列表为空,请等待一秒钟。此 *允许主线程运行并读取另一个线程 *如果列表不为空,请删除第一个 *计算等待的秒数——如果 *结果小于0(时间已过),然后设置 *睡眠时间为0。 */ 如果(报警==NULL) 睡眠时间=1; 否则{ 报警\列表=报警->链接; 现在=时间(空); 如果(报警->时间-现在); #ifdef调试 printf(“[等待:%d(%d)\%s\”]\n”,报警->时间, 睡眠时间、报警->信息); #恩迪夫 } /* *在等待之前解锁互斥锁,以便 *线程可以锁定它以插入新的报警请求。如果 *睡眠时间为0,然后调用sched_yield,给予 *主线程有机会运行,如果它已经 *由用户输入准备,不延迟消息 *如果没有输入。 */ 状态=pthread\u mutex\u unlock(&alarm\u mutex); 如果(状态!=0) err_abort(状态为“解锁互斥锁”); 如果(睡眠时间>0) 睡眠(睡眠时间); 其他的 sched_收益率(); /* *如果计时器过期,则打印消息并释放 *结构。 */ 如果(报警!=NULL){ printf((%d)%s\n),报警->秒,报警->消息); 免费(报警); } } } int main(int argc,char*argv[]) { 智力状态; 字符行[128]; 报警_t*报警,**上一个,*下一个; pthread\u t线程; 状态=pthread\u创建( &线程,空,报警线程,空); 如果(状态!=0) err_abort(状态为“创建报警线程”); 而(1){ printf(“报警>”); 如果(fgets(line,sizeof(line,stdin)==NULL)退出(0); 如果(strlen(line)秒,报警->消息)时间=时间(空)+报警->秒; /* *将新警报插入警报列表, *按过期时间排序。 */ 最后=&报警列表; next=*last; while(下一步!=NULL){ 如果(下一次->时间>=报警->时间){ 报警->链接=下一步; *最后=警报; 打破 } last=&next->link; 下一步=下一步->链接; } /* *如果我们到达列表的末尾,请插入新的 *报警。(“下一步”为空,“最后一步”为零 *到最后一项的链接字段,或到 *列表标题)。 */ if(next==NULL){ *最后=警报; 报警->链接=空; } #ifdef调试 printf(“[列表:”); for(下一步=报警列表;下一步!=NULL;下一步=下一步->链接) printf(“%d(%d)[\%s\”],下一次->时间, 下一步->时间-时间(空),下一步->消息); printf(“]\n”); #恩迪夫 状态=pthread\u mutex\u unlock(&alarm\u mutex); 如果(状态!=0) err_abort(状态为“解锁互斥锁”); } } }_C++_Mutex - Fatal编程技术网

C++ 互斥体/被锁定的互斥体数据是什么? #包括 #包括 #包括“errors.h” typedef结构报警标签{ 结构报警标签*链接; 整数秒; 从历元算起的时间;/*秒*/ 字符消息[64]; }警报; pthread\u mutex\u t alarm\u mutex=pthread\u mutex\u初始值设定项; 报警*报警列表=空; 无效*报警线程(无效*参数) { 警报_t*警报; 整夜睡眠时间; 现在是时候了; 智力状态; 而(1){ 状态=pthread\u mutex\u lock(&alarm\u mutex); 如果(状态!=0) err_abort(状态为“锁定互斥”); 报警=报警列表; /* *如果报警列表为空,请等待一秒钟。此 *允许主线程运行并读取另一个线程 *如果列表不为空,请删除第一个 *计算等待的秒数——如果 *结果小于0(时间已过),然后设置 *睡眠时间为0。 */ 如果(报警==NULL) 睡眠时间=1; 否则{ 报警\列表=报警->链接; 现在=时间(空); 如果(报警->时间-现在); #ifdef调试 printf(“[等待:%d(%d)\%s\”]\n”,报警->时间, 睡眠时间、报警->信息); #恩迪夫 } /* *在等待之前解锁互斥锁,以便 *线程可以锁定它以插入新的报警请求。如果 *睡眠时间为0,然后调用sched_yield,给予 *主线程有机会运行,如果它已经 *由用户输入准备,不延迟消息 *如果没有输入。 */ 状态=pthread\u mutex\u unlock(&alarm\u mutex); 如果(状态!=0) err_abort(状态为“解锁互斥锁”); 如果(睡眠时间>0) 睡眠(睡眠时间); 其他的 sched_收益率(); /* *如果计时器过期,则打印消息并释放 *结构。 */ 如果(报警!=NULL){ printf((%d)%s\n),报警->秒,报警->消息); 免费(报警); } } } int main(int argc,char*argv[]) { 智力状态; 字符行[128]; 报警_t*报警,**上一个,*下一个; pthread\u t线程; 状态=pthread\u创建( &线程,空,报警线程,空); 如果(状态!=0) err_abort(状态为“创建报警线程”); 而(1){ printf(“报警>”); 如果(fgets(line,sizeof(line,stdin)==NULL)退出(0); 如果(strlen(line)秒,报警->消息)时间=时间(空)+报警->秒; /* *将新警报插入警报列表, *按过期时间排序。 */ 最后=&报警列表; next=*last; while(下一步!=NULL){ 如果(下一次->时间>=报警->时间){ 报警->链接=下一步; *最后=警报; 打破 } last=&next->link; 下一步=下一步->链接; } /* *如果我们到达列表的末尾,请插入新的 *报警。(“下一步”为空,“最后一步”为零 *到最后一项的链接字段,或到 *列表标题)。 */ if(next==NULL){ *最后=警报; 报警->链接=空; } #ifdef调试 printf(“[列表:”); for(下一步=报警列表;下一步!=NULL;下一步=下一步->链接) printf(“%d(%d)[\%s\”],下一次->时间, 下一步->时间-时间(空),下一步->消息); printf(“]\n”); #恩迪夫 状态=pthread\u mutex\u unlock(&alarm\u mutex); 如果(状态!=0) err_abort(状态为“解锁互斥锁”); } } }

C++ 互斥体/被锁定的互斥体数据是什么? #包括 #包括 #包括“errors.h” typedef结构报警标签{ 结构报警标签*链接; 整数秒; 从历元算起的时间;/*秒*/ 字符消息[64]; }警报; pthread\u mutex\u t alarm\u mutex=pthread\u mutex\u初始值设定项; 报警*报警列表=空; 无效*报警线程(无效*参数) { 警报_t*警报; 整夜睡眠时间; 现在是时候了; 智力状态; 而(1){ 状态=pthread\u mutex\u lock(&alarm\u mutex); 如果(状态!=0) err_abort(状态为“锁定互斥”); 报警=报警列表; /* *如果报警列表为空,请等待一秒钟。此 *允许主线程运行并读取另一个线程 *如果列表不为空,请删除第一个 *计算等待的秒数——如果 *结果小于0(时间已过),然后设置 *睡眠时间为0。 */ 如果(报警==NULL) 睡眠时间=1; 否则{ 报警\列表=报警->链接; 现在=时间(空); 如果(报警->时间-现在); #ifdef调试 printf(“[等待:%d(%d)\%s\”]\n”,报警->时间, 睡眠时间、报警->信息); #恩迪夫 } /* *在等待之前解锁互斥锁,以便 *线程可以锁定它以插入新的报警请求。如果 *睡眠时间为0,然后调用sched_yield,给予 *主线程有机会运行,如果它已经 *由用户输入准备,不延迟消息 *如果没有输入。 */ 状态=pthread\u mutex\u unlock(&alarm\u mutex); 如果(状态!=0) err_abort(状态为“解锁互斥锁”); 如果(睡眠时间>0) 睡眠(睡眠时间); 其他的 sched_收益率(); /* *如果计时器过期,则打印消息并释放 *结构。 */ 如果(报警!=NULL){ printf((%d)%s\n),报警->秒,报警->消息); 免费(报警); } } } int main(int argc,char*argv[]) { 智力状态; 字符行[128]; 报警_t*报警,**上一个,*下一个; pthread\u t线程; 状态=pthread\u创建( &线程,空,报警线程,空); 如果(状态!=0) err_abort(状态为“创建报警线程”); 而(1){ printf(“报警>”); 如果(fgets(line,sizeof(line,stdin)==NULL)退出(0); 如果(strlen(line)秒,报警->消息)时间=时间(空)+报警->秒; /* *将新警报插入警报列表, *按过期时间排序。 */ 最后=&报警列表; next=*last; while(下一步!=NULL){ 如果(下一次->时间>=报警->时间){ 报警->链接=下一步; *最后=警报; 打破 } last=&next->link; 下一步=下一步->链接; } /* *如果我们到达列表的末尾,请插入新的 *报警。(“下一步”为空,“最后一步”为零 *到最后一项的链接字段,或到 *列表标题)。 */ if(next==NULL){ *最后=警报; 报警->链接=空; } #ifdef调试 printf(“[列表:”); for(下一步=报警列表;下一步!=NULL;下一步=下一步->链接) printf(“%d(%d)[\%s\”],下一次->时间, 下一步->时间-时间(空),下一步->消息); printf(“]\n”); #恩迪夫 状态=pthread\u mutex\u unlock(&alarm\u mutex); 如果(状态!=0) err_abort(状态为“解锁互斥锁”); } } },c++,mutex,C++,Mutex,嗨,这是我的代码,谁能告诉我,因为互斥体没有在结构中声明。因此,当互斥锁锁定和解锁时,有人能告诉我实际更改了哪些数据吗?pthread\u mutex\u t alarm\u mutex=pthread\u mutex\u初始值设定项创建用于锁定/解锁的共享互斥对象 pthread\u mutex\u lock在互斥锁可用时立即锁定它。执行此行后,它对所有其他线程都不可用。 pthread\u mutex\u unlock解锁互斥锁,使其再次可用于其他线程(解锁另一个线程的pthread\u m

嗨,这是我的代码,谁能告诉我,因为互斥体没有在结构中声明。因此,当互斥锁锁定和解锁时,有人能告诉我实际更改了哪些数据吗?

pthread\u mutex\u t alarm\u mutex=pthread\u mutex\u初始值设定项创建用于锁定/解锁的共享互斥对象

pthread\u mutex\u lock
在互斥锁可用时立即锁定它。执行此行后,它对所有其他线程都不可用。
pthread\u mutex\u unlock
解锁互斥锁,使其再次可用于其他线程(解锁另一个线程的
pthread\u mutex\u锁

受互斥锁保护的这组数据在哪里

互斥对象是报警互斥对象。受其“保护”的数据不必在代码中明确提及;如中所示,不需要语义连接。互斥体是一种低级线程原语,因此用户需要围绕它构建自己的逻辑。在您的情况下,内存中的一个位置用于阻止代码的其他部分(访问实际数据的部分)进行干扰


可以这样想:
std::atomic x表示其上操作的原子性<代码>整数x;互斥m要求访问
x
的每一段代码都正确查看
m
,以确保程序的正确性。这个低级访问就是我们在您的示例中看到的。

互斥体不知道它在保护什么。程序员的工作就是知道这一点,并且只在互斥锁被锁定时更改它所保护的数据


在这种特殊情况下,
报警列表似乎是被锁定的数据。

“嗨,这是我的代码,有人能告诉我它是做什么的吗?”-你是真的吗?正如我讲座幻灯片中的一样,我只是想知道你什么时候锁定互斥锁,线程编辑数据,受互斥锁保护的这组数据在哪里?因此它不是您的代码。为什么不问问编写它的人呢?那么当您执行pthread\u mutex\u lock(&thread)时,线程是否从那里开始运行?或者它是从pthread_create()的pthread_create()开始的。pthread_mutex_lock(&thread)wil
#include <pthread.h>
#include <time.h>
#include "errors.h"


    typedef struct alarm_tag {
struct alarm_tag    *link;
int                 seconds;
time_t              time;   /* seconds from EPOCH */
char                message[64];
    } alarm_t;

    pthread_mutex_t alarm_mutex = PTHREAD_MUTEX_INITIALIZER;
alarm_t *alarm_list = NULL;


void *alarm_thread (void *arg)
{
alarm_t *alarm;
int sleep_time;
time_t now;
int status;


while (1) {
    status = pthread_mutex_lock (&alarm_mutex);
    if (status != 0)
        err_abort (status, "Lock mutex");
    alarm = alarm_list;

    /*
     * If the alarm list is empty, wait for one second. This
     * allows the main thread to run, and read another
     * command. If the list is not empty, remove the first
     * item. Compute the number of seconds to wait -- if the
     * result is less than 0 (the time has passed), then set
     * the sleep_time to 0.
     */
    if (alarm == NULL)
        sleep_time = 1;
    else {
        alarm_list = alarm->link;
        now = time (NULL);
        if (alarm->time <= now)
            sleep_time = 0;
        else
            sleep_time = alarm->time - now;
#ifdef DEBUG
        printf ("[waiting: %d(%d)\"%s\"]\n", alarm->time,
            sleep_time, alarm->message);
#endif
        }

    /*
     * Unlock the mutex before waiting, so that the main
     * thread can lock it to insert a new alarm request. If
     * the sleep_time is 0, then call sched_yield, giving
     * the main thread a chance to run if it has been
     * readied by user input, without delaying the message
     * if there's no input.
     */
    status = pthread_mutex_unlock (&alarm_mutex);
    if (status != 0)
        err_abort (status, "Unlock mutex");
    if (sleep_time > 0)
        sleep (sleep_time);
    else
        sched_yield ();

    /*
     * If a timer expired, print the message and free the
     * structure.
     */
    if (alarm != NULL) {
        printf ("(%d) %s\n", alarm->seconds, alarm->message);
        free (alarm);
    }
}
}

int main (int argc, char *argv[])
{
        int status;
    char line[128];
    alarm_t *alarm, **last, *next;
    pthread_t thread;

status = pthread_create (
    &thread, NULL, alarm_thread, NULL);
if (status != 0)
    err_abort (status, "Create alarm thread");
while (1) {
    printf ("alarm> ");
    if (fgets (line, sizeof (line), stdin) == NULL) exit (0);
    if (strlen (line) <= 1) continue;
    alarm = (alarm_t*)malloc (sizeof (alarm_t));
    if (alarm == NULL)
        errno_abort ("Allocate alarm");

    /*
     * Parse input line into seconds (%d) and a message
     * (%64[^\n]), consisting of up to 64 characters
     * separated from the seconds by whitespace.
     */
    if (sscanf (line, "%d %64[^\n]", 
        &alarm->seconds, alarm->message) < 2) {
        fprintf (stderr, "Bad command\n");
        free (alarm);
    } else {
        status = pthread_mutex_lock (&alarm_mutex);
        if (status != 0)
            err_abort (status, "Lock mutex");
        alarm->time = time (NULL) + alarm->seconds;

        /*
         * Insert the new alarm into the list of alarms,
         * sorted by expiration time.
         */
        last = &alarm_list;
        next = *last;
        while (next != NULL) {
            if (next->time >= alarm->time) {
                alarm->link = next;
                *last = alarm;
                break;
            }
            last = &next->link;
            next = next->link;
        }
        /*
         * If we reached the end of the list, insert the new
         * alarm there. ("next" is NULL, and "last" points
         * to the link field of the last item, or to the
         * list header).
         */
        if (next == NULL) {
            *last = alarm;
            alarm->link = NULL;
        }
#ifdef DEBUG
        printf ("[list: ");
        for (next = alarm_list; next != NULL; next = next->link)
            printf ("%d(%d)[\"%s\"] ", next->time,
                next->time - time (NULL), next->message);
        printf ("]\n");
#endif
        status = pthread_mutex_unlock (&alarm_mutex);
        if (status != 0)
            err_abort (status, "Unlock mutex");
    }
}
    }