Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:35:26+00:00 2026-06-13T09:35:26+00:00

is there a known, working code to add elements to a shared, initialized c-style

  • 0

is there a known, working code to add elements to a shared, initialized c-style array (of objects) in multiple threads simultaneously without having to use lock on that resource? i mean, to a continuous memory block where elements are not linked by pointers.

i mean, algorithm, not libraries etc.
i just want to fill an array with elements in, say, 20 threads without locks or having to specify array ranges for threads where to fill (much like lockfree linked lists).

im using slackware 13.37 64 bit, pthreads, intel c++ compiler, tcmalloc allocator.

update : thanks guys for your answers, you all have been helpful with your ideas; if i could, i would mark all your ideas as answers. however, im still struggling with my code. so ill put the code up later in another thread to see whats happening exactly with my code, so people could really focus.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:35:28+00:00Added an answer on June 13, 2026 at 9:35 am

    Maybe I didn’t get the idea correctly, but the obvious solution is just to use std::atomic increment for current array index. Atomics on integral types are usually implemented as lock-free.

    But if this is not true or your compiler doesn’t support C++11 you could just replace it with your compiler-specific lock-free function, e.g. InterlockedIncrement for VS or __sync_fetch_and_add for GCC.

    For Intel C++ Compiler C++ 11 atomics are supported. Also you could use _InterlockedIncrement64 from ia64intrin.h header file, see page 147 of Intel(R) C++ Intrinsics Reference.

    Sample code (proof that it works here)

    #include <atomic>
    #include <thread>
    #include <iostream>
    
    const uint max_count = 100;
    
    std::atomic_uint count;
    std::string data[max_count];
    
    void thread_func(const char* str)
    {
       while(true)
       {
          const uint index = count++;
          if(index >= max_count) break;
          // Use += to see defect if data was already initialized by other thread
          data[index] += str;
          std::this_thread::sleep_for(std::chrono::milliseconds(1));
        }
     }
    
     int main()
     {
        std::cout << "Atomic counter is lock-free: " << 
           (count.is_lock_free() ? "Yes!" : "No!") << std::endl;
    
        std::thread t1(thread_func, "Thread 1");
        std::thread t2(thread_func, "Thread 2");
        std::thread t3(thread_func, "Thread 3");
    
        t1.join();
        t2.join();
        t3.join();
    
        for(uint i = 0; i < max_count; ++i)
        {
           std::cout<< i << ": " << data[i] << std::endl;
        }
    
        return 0;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there is a command that updates the changes like c:\svn up <working
I am working on templating in PHP. I know there are templating engines out
I am working on a Metro app, and as far as I know there
I've been working with app engine for quite some time, I know that there
Am working with knockout template binding, i just want to know is there any
is there any working, supported and maintained scaffolding solution for Wicket 1.5? I know
Are there known issues with storing user defined types within index organized tables in
Are there any known issues with ASP.Net Ajax and IE6 in Windows 2000 machines
Is there any known attack on this modified version of Yahalom? Cannot find anything...
Are there any known design principles, best-practices and design patterns that one can follow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.