Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
descriptor_queue old_queue, new_queue;
do {
old_queue = queue_head;
desc->Next = (descriptor*)old_queue.DescAvail;
new_queue.DescAvail = (unsigned __int64)desc;
new_queue.tag = old_queue.tag + 1;
} while (!compare_and_swap64k(queue_head, old_queue, new_queue));
#pragma pack(1) в первой структуре и наличие unsigned __int64 _pad0[8]; (да ещё два раза) во второй.top_aba_t (без этого же не работало, да?). В результате вместо 8-ми байтовой top_aba_t используется 130-байтовая структура.do {
long snapshot = stack->head;
long next = snapshot->next;
} while (!cas(&stack->head, next, snapshot));
old_top.ocount = queue->both.ocount;
old_top.top = queue->both.top;
#include <thread>
#include <assert.h>
struct test_t { union { struct { volatile int64_t a:48, b:16; }; int64_t i; }; };
test_t test;
void thread1()
{
while (true)
{
test_t x;
x.a = (test.a + 1) % 1000;
x.b = x.a;
test.i = x.i;
}
}
void thread2()
{
while (true)
{
{
test_t x;
x.a = test.a;
x.b = test.b;
if (x.a != x.b) printf("OMG 1, %ld != %ld\n", x.a, x.b);
}
{
test_t x = test;
if (x.a != x.b) printf("OMG 2, %ld != %ld\n", x.a, x.b);
}
{
test_t x; x.i = test.i;
if (x.a != x.b) printf("OMG 3, %ld != %ld\n", x.a, x.b);
}
}
}
int main()
{
assert(sizeof(test_t) == 8);
std::thread t1(thread1);
std::thread t2(thread2);
t1.join();
t2.join();
return 0;
}
#define compare_and_swap64(address, old_value, new_value) \
(InterlockedCompareExchange64(\
(PLONGLONG)(address), (__int64)(new_value), (__int64)(old_value)) \
== (__int64)(old_value))
априори известно, что он может не работать
union {
struct {
int a;
int b;
};
long long l;
} u1,u2;
u1.a=u2.a; u1.b=u2.b;
u1.l=u2.l;
Об одном методе распределения памяти