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);

  • 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);

  • Hi,

    You might've already seen this course, but I would recommend you to take a look at the fundamentals course we have at https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/. Topic 7 and topic 8 contains theory and hands on exercises (with solutions) about multithreaded applications and Thread synchronization. Topic 8, exercise 2 (with the solution here) even illustrates how to use mutexes to synchronize two threads that attempts to access the same code simultaneously.

    The mentioned topics should give you a hint as for why your implementation might not behave as you expected. As far as I can see the first thread is run once at the beginning, but never again after line 24. 

    Let me know if the link is helpful and if you have more questions regarding this topic.

    Kind regards,
    Andreas

     

  • Hi, Andreas.

    Yes, it is clear and useful, thanks we can close the ticket. I understand.

    best regards

    B.

Related