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
  • or this:

    /*
     * 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(m1);
    K_MUTEX_DEFINE(m2);
    
    /* work thread 1 */
    void ping()
    {
    	while (true)
    	{
    		k_mutex_lock(&m1, K_FOREVER);
    		printk("ping\n");
    		k_mutex_unlock(&m2);
    		k_busy_wait(100000);
    		k_msleep(2000);
    	}
    }
    /* work thread 2 */
    void pong()
    {
    	while (true)
    	{
    		k_mutex_lock(&m2, K_FOREVER);
    		printk("pong\n");
    		k_mutex_unlock(&m1);
    		k_busy_wait(100000);
    		k_msleep(2000);
    	}
    }
    
    void main(void)
    {
    	printk("WORK MUTEX on board: %s\n\n", CONFIG_BOARD);
    	k_mutex_lock(&m1, K_FOREVER);
    	k_mutex_unlock(&m2);
    }
    
    K_THREAD_DEFINE(thread_1_id, STACKSIZE, ping, NULL, NULL, NULL,
    				PRIORITY, 0, 500);
    K_THREAD_DEFINE(thread_2_id, STACKSIZE, pong, NULL, NULL, NULL,
    				PRIORITY, 0, 500);

Reply
  • or this:

    /*
     * 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(m1);
    K_MUTEX_DEFINE(m2);
    
    /* work thread 1 */
    void ping()
    {
    	while (true)
    	{
    		k_mutex_lock(&m1, K_FOREVER);
    		printk("ping\n");
    		k_mutex_unlock(&m2);
    		k_busy_wait(100000);
    		k_msleep(2000);
    	}
    }
    /* work thread 2 */
    void pong()
    {
    	while (true)
    	{
    		k_mutex_lock(&m2, K_FOREVER);
    		printk("pong\n");
    		k_mutex_unlock(&m1);
    		k_busy_wait(100000);
    		k_msleep(2000);
    	}
    }
    
    void main(void)
    {
    	printk("WORK MUTEX on board: %s\n\n", CONFIG_BOARD);
    	k_mutex_lock(&m1, K_FOREVER);
    	k_mutex_unlock(&m2);
    }
    
    K_THREAD_DEFINE(thread_1_id, STACKSIZE, ping, NULL, NULL, NULL,
    				PRIORITY, 0, 500);
    K_THREAD_DEFINE(thread_2_id, STACKSIZE, pong, NULL, NULL, NULL,
    				PRIORITY, 0, 500);

Children
No Data
Related