Mutex not unlock ?

Hi to all,

it's definitely a trivial thing but I can't get out of it.
Why don't I unlock the mutex and run the first thread?

Thank you.

/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/kernel.h>

#define STACKSIZE (1024)
#define PRIORITY 5

K_MUTEX_DEFINE(mutex);

uint8_t cnt = 0;
#define LOCK_CNT 10;

/* shered variable */
uint16_t counter = 0x00;
/* work thread 1 */
void thread_1()
{
	while (true)
	{
		k_mutex_lock(&mutex, K_FOREVER);
		counter++;
		printk("Thread: 1 work - shared cnt: %d\n", cnt);
		k_msleep(500);
	}
}
/* work thread 2 */
void thread_2()
{
	while (true)
	{
		counter++;
		printk("Thread: 2 work - shared cnt: %d\n", cnt);

		if (cnt-- == 0)
		{
			cnt = LOCK_CNT;
			k_mutex_unlock(&mutex);
			printk("mutex_count: %d\n", mutex.lock_count);
		}
		k_msleep(1000);
	}
}

void main(void)
{
	printk("WORK MUTEX on board: %s\n\n", CONFIG_BOARD);
	cnt = LOCK_CNT;
	k_mutex_lock(&mutex, K_FOREVER); /* lock mutex */
}

K_THREAD_DEFINE(thread_1_id, STACKSIZE, thread_1, NULL, NULL, NULL,
				PRIORITY, 0, 0);
K_THREAD_DEFINE(thread_2_id, STACKSIZE, thread_2, NULL, NULL, NULL,
				PRIORITY, 0, 0);

Parents Reply Children
No Data
Related