Multithreading in C Using Pthreads (part 2) - Order Violation Bugs

preview_player
Показать описание
Multithreading in C Using Pthreads (part 2) - Order Violation Bugs

In this video I illustrate an example of a very common multithreading bug called Order Violation, which is a type of non-deadlock bug.

We walk through the process of adding a mutex and a condition variable to fix the indeterminate behavior in the program. If you have any questions, comments or feedback, please leave them below in the comments!

GitHub Examples:

Рекомендации по теме
Комментарии
Автор

TOC:
Introduction: 00:00 - 1:17
Bugged Program Overview: 1:18 - 6:30
Fixing the Bugged Program: 6:31 - 15:10
Running the Program and Conclusion: 15:11 - 17:04

tech_with_moss
Автор

I've been grappling with C for a couple of years now, and I reckon I'm getting to the point where I'm just about beyond "beginner" level. Vids like this are *so* useful in advancing knowledge. Thank you!

bettyswunghole
Автор

Hey Moss, thanks for your last 2 videos on pthreads. I've recently been looking into multithreading in C for the first time, after previously only using Python. It's quite a step, but I found your two videos some of the most informative I've seen yet. Please keep them coming!

AlexColeAldems
Автор

Hi there I find your videoes really enjoying and super helpful, but i had to recheck some things i dont know as you didnt explain why do you transfer the lock to the pthread_cond_wait as parameter and what happens to locks when the worker2 thread goes to sleep. So just a refinement and thanks a lot for your help !
I found out in the documentation that the mutex has to be in the locked state when transferred or an error would occur, and when it sleeps it releases the mutex to be gained again when signaling on condition is achieved

tzvikaboguslavsky
Автор

How to create multiple threads to perform different each thread performing different operating by using one( user chaoice to create number of threads)but I have four functions

luckeylokesh
Автор

Very nice multithreading with mutex
Can you provide with semaphore with multi threading video

subbumotepalli
Автор

Very good, thanks. Just wondering what virtual machine you are using?

parsecscopeking
Автор

Basic question I'm a bit confused by: why are mutexes necessary at all? In the example, why couldn't you just use a global variable called msg_initialized and have the code in workerThreadFunc2 just wait until msg_initialized = 1 before proceeding?

timbaginski
Автор

Regarding pthread_cond_wait(), I had thought the worker function releases the lock and go sleep and wait AT THAT LINE until it gets signal and once it gets the signal then it will check the state value and lock the mutex again and proceed. But you said, when it gets the signal it executes from line 21, which means it locks again and proceed. Which one is correct?

chickentowel-ek
Автор

Question. In one core system, multiple threads actually run at the same time? or just one at a time by task scheduler but they "look like" run simultaneously?

chickentowel-ek
Автор

Thank you very much! You helped me a lot! <3 <3 <3 (-:

דיןה-לז
Автор

You don't need neither mutex nor conditional variable. This peace piece of code would also work and it's simpler:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

char *HELLO_MESSAGE;
int THREADS_CREATED = 0;

void *workerThreadFunc(void *tid) {
HELLO_MESSAGE = "Hello World!";
THREADS_CREATED = 1;
}

void *workerThreadFunc2(void *tid) {
while (THREADS_CREATED == 0) {

}
for (int ii = 0; ii < 13; ii++)
printf("%c\n", HELLO_MESSAGE[ii]);
}

int main() {
pthread_t tid0;
pthread_t tid1;

pthread_create(&tid0, NULL, &workerThreadFunc, (void*)&tid0);
pthread_create(&tid1, NULL, &workerThreadFunc2, (void*)&tid1);

pthread_exit(NULL);

return 0;
}

sasamilenkovic
visit shbcf.ru