Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #120424
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | a try_lock pattern... |
| Date | 2024-09-28 00:20 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <vd8an7$15ff9$1@dont-email.me> (permalink) |
This is just a quick test of my try_lock pattern thing. ct_shared::m_b
counts how many times extra work was able to be done instead of spinning
on the ct_shared::m_lock_a mutex. Take careful notice of the
ct_shared::work_a_lock() function.
Can you test it out? A large number for ct_shared::m_b is ideal. It
means extra work was being done. Avoids spinning in an interesting way.
my crude code, can you compile and run it okay? Thanks:
_________________________
#include <cassert>
#include <iostream>
#include <atomic>
#include <mutex>
#include <functional>
#include <thread>
#define CT_THREADS 10
#define CT_ITERS 1000000
// Test Threads...
struct ct_shared
{
long m_a;
unsigned long m_b;
std::mutex m_lock_a;
std::mutex m_lock_b;
ct_shared() : m_a(0), m_b(0) {}
bool work_b_try(unsigned long addend)
{
if (! m_lock_b.try_lock())
{
return false;
}
{
m_b += addend;
}
m_lock_b.unlock();
return true;
}
// The main pattern...
void work_a_lock()
{
while (! m_lock_a.try_lock())
{
if (! work_b_try(1))
{
m_lock_a.lock();
break;
}
}
}
void work_a_unlock()
{
m_lock_a.unlock();
}
void work_a(long addend)
{
work_a_lock();
{
m_a += addend;
}
work_a_unlock();
}
};
// Test out a pattern...
void
ct_thread(
ct_shared& shared
) {
for (unsigned long i = 0; i < CT_ITERS; ++i)
{
shared.work_a(1);
std::this_thread::yield();
shared.work_a(-1);
}
}
// Get things going...
int
main()
{
std::cout << "ct try_lock pattern Test...\n\n";
{
ct_shared shared;
std::thread threads[CT_THREADS];
// Create threads...
std::cout << "launching threads..." << std::endl;
for (unsigned long i = 0; i < CT_THREADS; ++i)
{
threads[i] = std::thread(ct_thread, std::ref(shared));
}
std::cout << "processing...\n" << std::endl;
// Join threads...
for (unsigned long i = 0; i < CT_THREADS; ++i)
{
threads[i].join();
}
// Sanity check...
{
std::cout << "shared.m_a = " << shared.m_a << "\n";
std::cout << "shared.m_b = " << shared.m_b << "\n";
if (shared.m_a != 0)
{
std::cout << "\n\nAAHHHH SHHHIIITT!!\n";
}
}
}
std::cout << "\nct try_lock pattern Test completed.\n\n";
return 0;
}
_________________________
What results do you get for ct_shared::m_b? Here is a result I get:
_____________________________
ct try_lock pattern Test...
launching threads...
processing...
shared.m_a = 0
shared.m_b = 8664093
ct try_lock pattern Test completed.
_____________________________
Well, shared.m_b is a high number... That is a lot potential kernel
calls that were skipped in favor of real work.
Any thoughts? Thanks.
Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread
a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-28 00:20 -0700
Re: a try_lock pattern... Paavo Helde <eesnimi@osa.pri.ee> - 2024-09-28 10:53 +0300
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-28 12:18 -0700
Re: a try_lock pattern... scott@slp53.sl.home (Scott Lurndal) - 2024-09-29 18:03 +0000
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-29 20:15 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 13:18 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-30 07:53 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-30 16:41 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-01 06:45 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 11:44 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-01 21:40 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 12:48 -0700
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 12:55 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-28 10:40 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-28 12:55 -0700
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-28 16:30 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-29 08:15 +0200
Re: a try_lock pattern... Muttley@dastardlyhq.com - 2024-09-29 09:01 +0000
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-29 12:54 +0200
Re: a try_lock pattern... Muttley@dastardlyhq.com - 2024-09-29 14:56 +0000
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-29 17:20 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 13:24 -0700
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 14:03 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-30 07:51 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 12:34 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-02 00:58 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 14:45 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-30 07:52 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-30 18:11 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-01 06:47 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 11:45 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-30 08:02 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-30 16:37 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-01 09:17 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 11:49 -0700
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 12:37 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-02 01:30 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 16:37 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-02 01:43 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 18:20 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-02 03:24 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-01 18:25 -0700
Re: a try_lock pattern... Bonita Montero <Bonita.Montero@gmail.com> - 2024-10-02 04:13 +0200
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-02 16:57 -0700
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 19:53 -0700
Re: a try_lock pattern... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-29 12:58 -0700
csiph-web