Practical example to barriers (pthread_barrier)

preview_player
Показать описание
Source code can be found here:

===== Support us through our store =====

===== Check out our website =====

===== Check out our Discord server =====
Рекомендации по теме
Комментарии
Автор

You are an amazing teacher. Love the way you simplify all such complex topics. Please make more videos... Like these on ompi and cuda as soon possible. Thanks a lot. May God bless you. 🤗

mohammadahsan
Автор

Wish you were on youtube back when I was in college.

stefantincescu
Автор

God bless you! ☺Thanks for the videos 👍

MichaelPims
Автор

thank you for the help. You are an amazing teacher !

algoeagle
Автор

Amazing video and very well taught! Thank you!

shashanksharma
Автор

there is something that I'm not understanding quite well,
I understood that the barrier makes the threads to wait until the barrier count of threads is satisfied,
but when u initialized the barrier with Thread_num+1 in main function and then used it in the roll_dice function
there are just 8 threads are going to enter the roll_dice function and wait at the barrier , but the barrier count is 9
so we have one last thread to satisfy the barrier count
u said that main is the last thread but how the main is gonna enter the roll_dice function and go through the barrier ?
I didn't understand this part quite well, if u could explain it, I appreciate it.
and thanks alot for your content, it's just amazing ❤

omarelnaggar
Автор

10:00 Also we could do calculation after "pthread_join" for loop. But I understand the problem. The real case could not be that simple.

marcinziajkowski
Автор

So if I am understanding this correctly, when you wanna know if a single thread has completed its execution of a particular code segment, you use condition variables to signal or broadcast, but when you wanna know if multiple threads has completed their execution of a common code segment, you use barriers?

Sandy-mvtl
Автор

Hi codevault, A question regarding the last parameter of barrier initialization.
Would waiting on these barrier basically do nothing?
pthread_barrier_init(&our_barrier1, NULL, 0);
pthread_barrier_init(&our_barrier2, NULL, 1);
Is there any reason to use 0 or 1?

ShaunYCheng
Автор

Man you are better than my college profesor. 
Why can not we use the barrierRolledDice again? Why do we have to create another one?
Also would broadcast do the work?

davidgrundfest
Автор

Wouldn't only using the 2nd barrier (barrierCalculated) work for us? Cause shouldn't we just have a barrier in the main thread when we have calculated what is the win condition and can then let threads check if they won. I ran the code and it works but if there is something else that might be happening do tell.

TinkuJain
Автор

Thank you for example. Can you please show some examples for linux io_uring. Io_uring makes sync call async.

ForeverNils
Автор

Can we implement this example with the signals?

novigradmerchant
Автор

Dear CodeVault, Im trying to compile using the pthread library and its having trouble to recognize pthread_barrier_t. And in man pthread barriers don't show up. So where do they come from? Thanks in advanced (;

titorrito-productions
Автор

At 11:10, why can't we use semaphores for this purpose?

onursimsek
Автор

Can''t we use also condition variable broadcast instead of barrierCalculated?

MrCrazyDuck
Автор

Thanks for your incredible videos. Would you help me answer the above questions:
In ex1.c (6.1), which of the following properties achieved:

(1) Mutual exclusion but not progress

(2) Progress but not mutual exclusion

(3) Neither mutual exclusion nor progress

(4) Both mutual exclusion and progress

Please explain?

1.2

To which arguments (in 6.1) is correct and which does not:

(1) always exits. when threads = 2 or threads <= 0

(2) always hangs. threads = 1 or thread > 2

for the above code:


int S1 = 0;
int S2 = 0;
int x = 0;
int run = 1;
void Producer(void) {
while(run) {
while (S1 == S2);
x++;
__sync_synchronize();
S1 = S2;
__sync_synchronize();
}
}
void Consumer(void) {
while(run) {
while (S1 != S2);
x--;
__sync_synchronize();
S1 = !S2;
__sync_synchronize();
}
}
void* Worker(void *func) {
long func_id = (long)func & 0x1;
printf("%s %d\n", __func__, (int)func_id);
switch (func_id) {
case 0:
Producer();
break;
case 1:
Consumer();
break;
}
return NULL;
}
int main(int argc, char *argv[]) {
pthread_t t[argc];
pthread_attr_t at;
cpu_set_t cpuset;
int threads;
int i;
#define MAX_PROCESSORS 4 // Minimal processors is 2.
threads = argc > 1 ? (( atoi(argv[1]) < 4) ? atoi(argv[1]):
MAX_PROCESSORS ) : 1;

for (i = 0;i < threads; i++){
CPU_ZERO(&cpuset);
CPU_SET(i, &cpuset);
pthread_attr_init(&at);
pthread_attr_setaffinity_np(&at, sizeof(cpuset), &cpuset);
if (pthread_create(&t[i], &at, Worker, (void *) (long)i) ) {
perror("pthread create 1 error\n"); }
}
do {
sleep(1);
} while(x < 0);
run = 0;
void *val;
for(i = 0; i < threads; i++)
pthread_join(t[i], &val);
printf("x=%d\n", x);
}

yasminamran