Skip to content

Potential error due to the released lock &init_mutex before program eixt #343

@jenny-cheung

Description

@jenny-cheung

Hi, developers, a small error (could be harmless) happen when the program exit with the lock &init_mutex unreleased. The fix is to insert pthread_mutex_unlock(&init_mutex); before exit(0). Could be fixed for high code quality. Thank you!

pthread_mutex_lock(&init_mutex);

int
main(int argc, char *argv[])
{
...;

	/* Hold lock until we are ready for the child threads to exit. */
	pthread_mutex_lock(&init_mutex);

	rc = pthread_create(&thread1, NULL, (void *(*) (void *)) func_call_1, NULL);
	if (rc != 0)
	{
		fprintf(stderr, "Failed to create thread 1: %s **\nexiting\n",
				strerror(errno));
		exit(1); // exit without releasing the lock
	}
	rc = pthread_create(&thread2, NULL, (void *(*) (void *)) func_call_2, NULL);
	if (rc != 0)
	{
		/*
		 * strerror() might not be thread-safe, and we already spawned thread
		 * 1 that uses it, so avoid using it.
		 */
		fprintf(stderr, "Failed to create thread 2 **\nexiting\n");
		exit(1); // exit without releasing the lock
	}

	...;
}

Best,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions