本文整理汇总了C++中std::atomic_int类的典型用法代码示例。如果您正苦于以下问题:C++ atomic_int类的具体用法?C++ atomic_int怎么用?C++ atomic_int使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了atomic_int类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TEST
TEST(JobSystem, JobSystemParallelChildren) {
v = 0;
JobSystem js;
js.adopt();
struct User {
std::atomic_int calls = {0};
void func(JobSystem&, JobSystem::Job*) {
v++;
calls++;
};
} j;
JobSystem::Job* root = js.createJob<User, &User::func>(nullptr, &j);
for (int i=0 ; i<256 ; i++) {
JobSystem::Job* job = js.createJob<User, &User::func>(root, &j);
js.run(job);
}
js.runAndWait(root);
EXPECT_EQ(257, v.load());
EXPECT_EQ(257, j.calls);
js.emancipate();
}
示例2: testit
void testit(std::atomic_int& count)
{
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1, 10);
auto sleep_time = std::bind(distribution, generator);
std::this_thread::sleep_for(std::chrono::microseconds(sleep_time()));
++count;
if(count.load() == 5) {
g_condition.notify_one();
}
}
示例3: main
int main()
{
Barrier barrier1(3);
Barrier barrier2(3);
arrived_all = false;
processed_all = true;
arrived_count.store(0);
processed_count.store(allowed_max);
std::thread worker[50];
for (int i = 0; i < 6; i++)
worker[i] = std::thread(worker_thread, i, &barrier1, &barrier2);
{
std::lock_guard<std::mutex> lk(m);
std::cout << "Back in main(), after initialing first thread batch " << data << std::endl;
}
// wait until worker dies finishes execution
for (int i = 0; i < 6; i++)
worker[i].join();
// for (int i = 5; i < 10; i++)
// worker[i] = std::thread(worker_thread, i);
// {
// std::lock_guard<std::mutex> lk(m);
// std::cout << "Back in main(), after initialing second thread batch." << std::endl;
// }
// // wait until worker dies finishes execution
// for (int i = 5; i < 10; i++)
// worker[i].join();
std::cout << "finished main(), data = " << data << std::endl;
}
示例4: sleep
void spring_time::sleep(bool forceThreadSleep)
{
if (forceThreadSleep) {
spring::this_thread::sleep_for(chrono::nanoseconds(toNanoSecsi()));
return;
}
// for very short time intervals use a yielding loop (yield is ~5x more accurate than sleep(), check the UnitTest)
if (toMicroSecsi() < (avgThreadSleepTimeMicroSecs + avgThreadYieldTimeMicroSecs * 5)) {
const spring_time s = gettime();
while ((gettime() - s) < *this)
thread_yield();
return;
}
// expected wakeup time
const spring_time t0 = gettime() + *this;
spring::this_thread::sleep_for(chrono::nanoseconds(toNanoSecsi()));
const spring_time t1 = gettime();
const spring_time dt = t1 - t0;
if (t1 >= t0) {
// yes, it's not 100% thread correct, but it's okay when 1 of 1 million writes is dropped
int avg = avgThreadSleepTimeMicroSecs.load();
int newAvg = mix<float>(avg, dt.toMicroSecsf(), 0.1f);
avgThreadSleepTimeMicroSecs.store(newAvg);
}
}
示例5: update
inline bool update()
{
if (windowId.load() == s_currentNativeWindowId.load())
return false;
s_currentNativeWindowMutex.lock();
window = s_currentNativeWindow;
windowId.store(s_currentNativeWindowId.load());
s_currentNativeWindowMutex.unlock();
return true;
}
示例6: stepLeft
void stepLeft() {
std::unique_lock<std::mutex> lock(mutex);
for (int i = 0; i < 10; ++i) {
std::cout << "left" << std::endl;
isWaiting.fetch_add(1);
if (isWaiting.load() % 2 != 0) {
condVar.notify_one();
}
else {
condVar.wait(lock);
}
}
}
示例7: thread_yield
static void thread_yield()
{
const spring_time t0 = spring_time::gettime();
this_thread::yield();
const spring_time t1 = spring_time::gettime();
const spring_time dt = t1 - t0;
if (t1 >= t0) {
// yes, it's not 100% thread correct, but it's okay when 1 of 1 million writes is dropped
int avg = avgThreadYieldTimeMicroSecs.load();
int newAvg = mix<float>(avg, dt.toMicroSecsf(), 0.1f);
avgThreadYieldTimeMicroSecs.store(newAvg);
}
}
示例8: HAL_CleanNotifier
void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
{
std::lock_guard<priority_recursive_mutex> sync(notifierMutex);
auto notifier = notifierHandles.Get(notifierHandle);
if (!notifier) return;
// remove from list
if (notifier->prev) notifier->prev->next = notifier->next;
if (notifier->next) notifier->next->prev = notifier->prev;
if (notifiers == notifier) notifiers = notifier->next;
notifierHandles.Free(notifierHandle);
if (notifier->threaded) {
NotifierThreadOwner* owner =
static_cast<NotifierThreadOwner*>(notifier->param);
delete owner;
}
}
if (notifierRefCount.fetch_sub(1) == 1) {
std::lock_guard<priority_mutex> sync(notifierInterruptMutex);
// if this was the last notifier, clean up alarm and manager
if (notifierAlarm) {
notifierAlarm->writeEnable(false, status);
notifierAlarm = nullptr;
}
if (notifierManager) {
notifierManager->disable(status);
notifierManager = nullptr;
}
closestTrigger = UINT64_MAX;
}
}
示例9: onSurfaceDestroyed
static void onSurfaceDestroyed(JNIEnv */*env*/, jobject /*object*/)
{
s_currentNativeWindowMutex.lock();
s_currentNativeWindow.reset();
s_currentNativeWindowId.store(0);
s_currentNativeWindowMutex.unlock();
}
示例10: while
Texture TextureBuilder::Build2DTexture(TextureBindpoint target, unsigned width, unsigned height, unsigned count,
unsigned channels, TexturePixelFormat pixelFormat, bool useMipmaps)
{
int current_id = id.fetch_add(1, std::memory_order_relaxed);
Texture result;
result.info.width = width;
result.info.height = height;
result.info.count = count;
result.info.type = TextureType::Tex2D;
result.info.target = target;
result.info.targetPixelFormat = pixelFormat;
result.info.channels = channels;
result.info.name = std::string(NAME_PREFIX) + std::to_string(current_id);
unsigned minSize = std::min(width, height);
if (useMipmaps)
{
unsigned i = 0;
while (minSize > 1)
{
minSize /= 2;
i++;
}
result.info.mipmaps = i;
}
return result;
}
示例11: initializeNotifier
void* initializeNotifier(void (*process)(uint64_t, void*), void *param, int32_t *status)
{
if (!process) {
*status = NULL_PARAMETER;
return nullptr;
}
if (!notifierAtexitRegistered.test_and_set())
std::atexit(cleanupNotifierAtExit);
if (notifierRefCount.fetch_add(1) == 0) {
std::lock_guard<priority_mutex> sync(notifierInterruptMutex);
// create manager and alarm if not already created
if (!notifierManager) {
notifierManager = new tInterruptManager(1 << kTimerInterruptNumber, false, status);
notifierManager->registerHandler(alarmCallback, NULL, status);
notifierManager->enable(status);
}
if (!notifierAlarm) notifierAlarm = tAlarm::create(status);
}
std::lock_guard<priority_recursive_mutex> sync(notifierMutex);
// create notifier structure and add to list
Notifier* notifier = new Notifier();
notifier->prev = nullptr;
notifier->next = notifiers;
if (notifier->next) notifier->next->prev = notifier;
notifier->param = param;
notifier->process = process;
notifiers = notifier;
return notifier;
}
示例12: cleanNotifier
void cleanNotifier(void* notifier_pointer, int32_t *status)
{
{
std::lock_guard<priority_recursive_mutex> sync(notifierMutex);
Notifier* notifier = (Notifier*)notifier_pointer;
// remove from list and delete
if (notifier->prev) notifier->prev->next = notifier->next;
if (notifier->next) notifier->next->prev = notifier->prev;
if (notifiers == notifier) notifiers = notifier->next;
delete notifier;
}
if (notifierRefCount.fetch_sub(1) == 1) {
std::lock_guard<priority_mutex> sync(notifierInterruptMutex);
// if this was the last notifier, clean up alarm and manager
if (notifierAlarm) {
notifierAlarm->writeEnable(false, status);
delete notifierAlarm;
notifierAlarm = nullptr;
}
if (notifierManager) {
notifierManager->disable(status);
delete notifierManager;
notifierManager = nullptr;
}
closestTrigger = UINT64_MAX;
}
}
示例13: print
void print(){
if(size==0)
printf("<empty>\n");
for(int i = 0; i < size;i++)
printf("%d ", arr[(head.load() + i)%capacity]);
printf("\n");
}
示例14: worker_thread
void worker_thread(int i, class Barrier& barrier1, class Barrier& barrier2)
{
{
// std::lock_guard<std::mutex> lk(m);
std::lock_guard<std::mutex> lk_guard(m);
std::cout << "Worker thread " << i << " has arrived." << std::endl;
}
barrier1.wait();
{
std::lock_guard<std::mutex> lk_guard(m);
std::cout << "counts changing2, processed_all: " << processed_all << ", count > max:" << (arrived_count < allowed_max) << std::endl;
arrived_count++;
processed_count--;
std::cout << "counts changing3, processed: " << processed_count << ", arrived:" << arrived_count<< std::endl;
}
{
std::lock_guard<std::mutex> lk_guard(m);
if (arrived_count.load() >= allowed_max) {
std::cout << "Enough arrived, about to open floodgate #1" << std::endl;
arrived_all = true;
processed_all = false;
// for (int i = 0; i < allowed_max; i++) {
// cv1.notify_one();
// }
}
std::cout << "about to reach 2nd barrier:" << arrived_count<< std::endl;
}
// std::unique_lock<std::mutex> lk1(barrier1);
barrier2.wait();
// cv1.wait(lk1, []{return (processed_count < allowed_max) && arrived_all;});
{
std::lock_guard<std::mutex> lk_guard(m);
processed_count++;
arrived_count--;
}
// critical section would go here... then increment processed_count
// report after critical section
{
std::lock_guard<std::mutex> lk(m);
std::cout << "Worker thread " << i << " data processing completed" << std::endl;
if (processed_count == allowed_max) {
std::cout << "Group finished, about to open floodgate #2" << std::endl;
processed_all = true;
arrived_all = false;
// for (int i = 0; i < allowed_max; i++) {
// cv2.notify_one();
// }
}
}
}
示例15:
Order::Order(int clientAssignedId, const std::string& account, const std::string& security,
double price, int amount, Operation operation, OrderType type) :
m_id(gs_id.fetch_add(1)),
m_clientAssignedId(clientAssignedId),
m_account(account),
m_security(security),
m_price(price),
m_amount(amount),
m_operation(operation),
m_type(type),
m_state(State::Unsubmitted)
{
}