Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sbitx_sound.c: sound_thread_start function: possible code 'smell' #38

Open
g8kig opened this issue Apr 29, 2023 · 5 comments
Open

sbitx_sound.c: sound_thread_start function: possible code 'smell' #38

g8kig opened this issue Apr 29, 2023 · 5 comments

Comments

@g8kig
Copy link

g8kig commented Apr 29, 2023

Can you clarify the purpose of the sleep(1) in the sound_thread_start function.

	pthread_create( &sound_thread, NULL, sound_thread_function, (void*)device);
	sleep(1);
	pthread_create( &loopback_thread, NULL, loopback_thread_function, (void*)device);

If this is attempting to get the sound_thread function running before loopback_thread runs it will not work as intended.
This is because there are a large number of threads potentially runnable as well as sound_thread.
Using sleep or any of the related functions is always a code 'smell'

@afarhan
Copy link
Owner

afarhan commented Apr 29, 2023

i wrote that to allow the first thread to open up the alsa device before the loopback is initialized. what alternative do you suggest?

@g8kig
Copy link
Author

g8kig commented Apr 29, 2023

  1. Declare a condition variable and a mutex along with the thread 'handles'
pthread_t sound_thread, loopback_thread;
static pthread_mutex_t sound_mutex;
static pthread_cond_t sound_cond_var;

  1. Initialise the mutex and condition variable at the same time as creating the threads
pthread_mutex_init(&sound_mutex, NULL);
pthread_cond_init(&sound_cond_var, NULL);
pthread_create( &sound_thread, NULL, sound_thread_function, device);

Note: Any of these functions could potentially fail. Best practice would check the return codes (if any).

3). In the thread that must wait for the alsa device to be opened before the loopback is initialised, wait on the condition variable

 pthread_mutex_lock(&sound_mutex);
 pthread_cond_wait(&sound_cond_var, &sound_mutex);
 pthread_mutex_unlock(&sound_mutex);

note: pthead_cond_wait unlocks and locks the mutex as required.

  1. In the sound thread when the alsa device is open, signal the condition variable allowing the loopback thread to run
 pthread_mutex_lock(&sound_mutex);
 pthread_cond_signal(&sound_cond_var);
 pthread_mutex_unlock(&sound_mutex);

5). Correctly the threads, condition variables should be closed (finalisation) however everything should be cleaned up when the program exits.

pthread_join(sound_thread, NULL);
pthread_join(loopback_thread, NULL);
pthread_cond_destroy(&sound_cond_var);
pthread_mutex_destroy(&sound_mutex);

@rafael2k
Copy link
Contributor

rafael2k commented May 4, 2023

I agree this is good solution. For less code verbosity, an atomic variable and spinlock could be used too. G8KIG, could you make a PR? I could review it.

@g8kig
Copy link
Author

g8kig commented May 4, 2023

It's already done and tested in my fork. I will do a PR for everything when things settle down.

@rafael2k
Copy link
Contributor

rafael2k commented May 4, 2023

Cool! Just realized about your fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants